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...
}
});
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
getLayoutManager.findViewByPosition(...) is called at the wrong time
Use view tree listening to get the corresponding controls or properties:
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