RecyclerView获取ItemView方法

RecyclerView获取ItemView方法实际比较简单,直接用LayoutManager实例的方法:

// positions是RecyclerView中每个item的位置
View view = layoutManager.findViewByPosition(position);

但是在fragment中如果直接调用上面的方法,返回的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);
    }
};

你可能感兴趣的:(RecyclerView获取ItemView方法)