上から特定のオフセットにあるリスト項目のインデックスを取得する簡単な方法はありますか? たとえば、上から 150 ピクセルに表示されるアイテムのインデックスを取得します。
1016 次
2 に答える
1
あなたの目標は、どのリスト項目が画面の中央にあるかを見つけることなので、次のようなことを試すことができます:
(MyListView.java など、ListView を拡張するカスタム クラスを作成します)
public class MyListView extends ListView implements OnScrollListener {
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
setOnScrollListener(this);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Center the child currently closest to the center of the ListView when
// the ListView stops scrolling.
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
int listViewCenterX = getWidth() / 2;
int listViewCenterY = getHeight() / 2;
Rect rect = new Rect();
// Iterate the children and find which one is currently the most
// centered.
for (int i = 0; i < getChildCount(); ++i) {
View child = getChildAt(i);
child.getHitRect(rect);
if (rect.contains(listViewCenterX, listViewCenterY)) {
// this listitem is in the "center" of the listview
// do what you want with it.
final int position = getPositionForView(child);
final int offset = listViewCenterY - (child.getHeight() / 2);
break;
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
}
于 2013-03-13T17:15:48.043 に答える
0
子を反復処理して、それぞれのヒット長方形をポイントに対してチェックできます。
于 2013-03-13T17:04:35.180 に答える