RecyclerView獲取ItemView方法實際比較簡單,直接用LayoutManager實例的方法:
// positions是RecyclerView中每個item的位置
View view = layoutManager.findViewByPosition(position);
但是在fragment中如果直接調(diào)用上面的方法,返回的view為空,我們需要用handler的postDelayed方法延遲獲取。
static Handler handler = new Handler(Looper.getMainLooper());
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
handler.postDelayed(runnable, 200);
}
Runnable runnable = new Runnable() {
@Override
public void run() {
View view = layoutManager.findViewByPosition(position);
}
};