Is there a better way of getting a reference to the parent RecyclerView from the adapter?

前端 未结 2 1764
傲寒
傲寒 2021-01-31 00:55

I have a use case where I need a reference to the parent RecyclerView from inside the adapter, specifically inside the onBindViewHolder method. So far

2条回答
  •  暖寄归人
    2021-01-31 01:34

    Another way is passing a reference in the constructor, e.g.

    public final class MyAdapter extends RecyclerView.Adapper {
        private final recyclerView;
    
        public MyAdapter(@NonNull RecyclerView recyclerView) {
            this.recyclerView = recyclerView;
        }
    
        ...
    
        @Override
         public void onBindViewHolder(ViewHolder holder, int position) {
            ...
        }
    }
    

提交回复
热议问题