2013-03-27 6 views
5

水平スクロールビューで線形レイアウトを追加し、レイアウトでいくつかのテキストビューを追加しました。このレイアウトで目に見える子供を得ることは可能ですか?Horizo​​ntalScrollView可視の子を取得する

このコードは、すべての子を取得しますが、私はのみ表示(現在表示し)子供を取得したい:

final HorizontalScrollView scroll = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1); 
    LinearLayout linearLayout = ((LinearLayout)scroll.findViewById(R.id.linearLayout1)); 
    int chilrenNum = linearLayout.getChildCount(); 
+0

あなたは 'visible child is setVisibilty(View.VISIBLE)'を意味しますか?あなたは「目に見える」という意味ですか? –

+0

私は目に見える目を見ます。 – user2106897

答えて

3

さて、上の検索のビットの後SO私はこの答えは、イベントをスクロールする聞くことがわかりました。 Implement Scroll Event Listener in Android。 あなたのScrollViewonScrollChangedを無効にし、あなたのスクロールビューの目に見える部分をあなたの行動にとどめておくことです。あなたは簡単にこのようなコードで見える景色を得ることができることを行う

:上記のすべてのコード

int currentPosition = lastXPosition; // lastXPosition gets updated from scroll event 
int layoutWidth = linearLayout.getWidth(); 
Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x; 
int childWidth = layoutWidth/linearLayout.getChildCount(); 
int firstVisibleXPos = currentPosition - width/2; // currentPosition will be in the middle of the screen 
int lastVisibleXPos = currentPosition + width/2; 

int indexOfFirstVisible = firstVisibleXPos/childWidth; 
int indexOfLastVisible = lastVisibleXPos/ childWidth; 

は固定子ビューのサイズを想定しています。可変の子のサイズを使用している場合は、最初に幅を取得して追跡し、親ビューのインデックスと位置に基づいて可視性を計算する必要があります。

+0

この行にエラーが表示されます。display.getSize(size); – user2106897

+0

私はimport android.view.Displayを使用しました。 – user2106897

+0

try int width = display.getWidth();代わりにあなたは2.xレベルに対してビルドしているようです –

関連する問題