How to pass or send data from recyclerview adapter to fragment

前端 未结 5 1374
执念已碎
执念已碎 2021-01-16 12:20

How to pass data from recyclerview adapter to fragment and what i have to write the code in fragment to receive data.I have already check the links in this website but unfor

5条回答
  •  终归单人心
    2021-01-16 12:36

    You have "ClickListener" in fragment.

    for example.

    @Override
    public void onResume(){
        super.onResume();
        ((ExampleAdapter) mAdapter).onItemClick(new RecyclerViewItemClick() {
            @Override
            public void onItemClick(int position, View v) {
                //to do. ArrayList<> data.get(position).getYourItem();
            }
        });
    }
    

    And you must create interface RecyclerViewItemClick.java

    public interface RecyclerViewItemClick {
        public void onItemClick(int position, View v);
    }
    

    Last. ExampleAdapter.java add

    public void onItemClick(RecyclerViewItemClick mclick){
       this.myClickListener = mclick;
    }
    

    ExampleViewHolder method add in

    @Override
    public void onClick(View v) {
        myClickListener.onItemClick(getAdapterPosition(), v);
        notifyDataSetChanged();
    }
    

    I hope you solve this problem. Thank.

提交回复
热议问题