页面结构:activity-fragment—recycleView
目标,整个页面蒙层,并在recycleView的第一个item上添加一个高亮提示。
现在我要获取这个item的view,在onResume中使用getLayoutManager.findViewByPosition方法得到的view为空。
我应该何时何地获取该view?
PHP中文网2017-04-18 09:05:54
getLayoutManager.findViewByPosition(...) is called at the wrong time
getView().post(new Runnable() {
View view = getLayoutManager.findViewByPosition(0);
// TODO ...
});
大家讲道理2017-04-18 09:05:54
Use view tree listening to get the corresponding controls or properties:
//1.开启视图树监听
recycleView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//2.视图绘制完毕,取出第一个item
View view = recyclerView.getChildAt(0);
//3.注销监听
recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//4. do something with the first itemview...
}
});
ringa_lee2017-04-18 09:05:54
It is possible to use the view tree monitoring method upstairs. Use getViewTreeObserver().addOnGlobalLayoutListener() to obtain the final width or height of the View, and getViewTreeObserver().addOnDrawListener to monitor the redrawing of the View. This is a common method