获取第一组Expandable ListView的View句柄
Get handle of View of first group of Expandable ListView
在 Activity
中,如何访问 Expandable ListView
的第一个群组视图?
这是我正在做的事情:
public int getFirstVisibleGroup() {
LogUtil.i(TAG, "getFirstVisibleGroup called");
int firstVis = listView.getFirstVisiblePosition();
LogUtil.i(TAG, "firstVis = " + firstVis);
long packedPosition = listView.getExpandableListPosition(firstVis);
LogUtil.i(TAG, "packedPosition = " + packedPosition);
LogUtil.i(TAG, "firstVisibleGroup = " + ExpandableListView.getPackedPositionGroup(packedPosition));
return ExpandableListView.getPackedPositionGroup(packedPosition);
}
public View getGroupView(ExpandableListView listView, int groupPosition) {
LogUtil.i(TAG, "getGroupView called");
int flatPosition = listView.getFlatListPosition(groupPosition);
LogUtil.i(TAG, "flatPosition = " + flatPosition);
int first = getFirstVisibleGroup();
LogUtil.i(TAG, "first = " + first);
LogUtil.i(TAG, "returning child at position " + (flatPosition - first));
return listView.getChildAt(flatPosition - first);
}
我这样称呼它:
View view = getGroupView(listView, 0);
最终变成listView.getChildAt(0)
。返回的 view
为空。
正确的做法是什么?
所有基于适配器的视图(ListView、GridView、RecyclerView)仅在将视图布局到屏幕后才将视图添加到自身。这样他们就可以计算出适当的大小并查询足够的子视图。
因此,在 onCreate
期间,您永远不会有任何观看次数。这意味着如果你想与它的一些子视图交互,它必须在以后发生。
一种合适的方法是使用 OnPreDraw
侦听器。那是在系统对视图调用 draw(canvas)
之前。示例:
public MyActivity extends Activity implements ViewTreeObserver.OnPreDrawListener {
@Override
public void onCreate(bundle){
... build your layout and your listview
// during onCreate you add a PreDrawListener
rootView.getViewTreeObserver().addOnPreDrawListener(this);
}
@Override
public void onPreDraw() {
... do your logic here !!!
rootView.getViewTreeObserver().removeOnPreDrawListener(this); // remove itself, you only need the fist pass
return true; // must return true, or else the system won't draw anything.
}
}
在 Activity
中,如何访问 Expandable ListView
的第一个群组视图?
这是我正在做的事情:
public int getFirstVisibleGroup() {
LogUtil.i(TAG, "getFirstVisibleGroup called");
int firstVis = listView.getFirstVisiblePosition();
LogUtil.i(TAG, "firstVis = " + firstVis);
long packedPosition = listView.getExpandableListPosition(firstVis);
LogUtil.i(TAG, "packedPosition = " + packedPosition);
LogUtil.i(TAG, "firstVisibleGroup = " + ExpandableListView.getPackedPositionGroup(packedPosition));
return ExpandableListView.getPackedPositionGroup(packedPosition);
}
public View getGroupView(ExpandableListView listView, int groupPosition) {
LogUtil.i(TAG, "getGroupView called");
int flatPosition = listView.getFlatListPosition(groupPosition);
LogUtil.i(TAG, "flatPosition = " + flatPosition);
int first = getFirstVisibleGroup();
LogUtil.i(TAG, "first = " + first);
LogUtil.i(TAG, "returning child at position " + (flatPosition - first));
return listView.getChildAt(flatPosition - first);
}
我这样称呼它:
View view = getGroupView(listView, 0);
最终变成listView.getChildAt(0)
。返回的 view
为空。
正确的做法是什么?
所有基于适配器的视图(ListView、GridView、RecyclerView)仅在将视图布局到屏幕后才将视图添加到自身。这样他们就可以计算出适当的大小并查询足够的子视图。
因此,在 onCreate
期间,您永远不会有任何观看次数。这意味着如果你想与它的一些子视图交互,它必须在以后发生。
一种合适的方法是使用 OnPreDraw
侦听器。那是在系统对视图调用 draw(canvas)
之前。示例:
public MyActivity extends Activity implements ViewTreeObserver.OnPreDrawListener {
@Override
public void onCreate(bundle){
... build your layout and your listview
// during onCreate you add a PreDrawListener
rootView.getViewTreeObserver().addOnPreDrawListener(this);
}
@Override
public void onPreDraw() {
... do your logic here !!!
rootView.getViewTreeObserver().removeOnPreDrawListener(this); // remove itself, you only need the fist pass
return true; // must return true, or else the system won't draw anything.
}
}