Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

java - How to pass or send data from recyclerview adapter to fragment

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 unfortunately unable to get correct code. Below is the code

ExampleAdapter.Java

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
    private ArrayList<ExampleItem> mExampleList;
    @NonNull
    @Override
    public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.example_item,viewGroup,false);
        ExampleViewHolder evh=new ExampleViewHolder(v);
        return evh;
    }
    @Override
    public void onBindViewHolder(@NonNull ExampleViewHolder exampleViewHolder, int i) {
        final ExampleItem currentItem=mExampleList.get(i);
        exampleViewHolder.mTextView.setText(currentItem.getmText());
        exampleViewHolder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // pass the data to the fragment
            }
        });
    }
    @Override
    public int getItemCount() {
        return mExampleList.size();
    }
    public static class ExampleViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ExampleViewHolder(@NonNull View itemView) {
            super(itemView);
            mTextView=itemView.findViewById(R.id.texttitle);
        }
    }
    public ExampleAdapter(ArrayList<ExampleItem> exampleList){
              this.mExampleList=exampleList;
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can pass data by attaching a callback into your adapter:

Create an interface:

interface OnTextClickListener {
    void onTextClick(ExampleItem data);
}

Delegate the callback:

class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {

    List<ExampleItem> items;
    OnTextClickListener listener;

    ExampleAdapter(List<ExampleItem> items, OnTextClickListener listener) {
        this.items = items;
        this.listener = listener;
    }

    @Override
    public void onBindViewHolder(ExampleViewHolder viewHolder, int position) {
        viewHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Say you want to pass an ExampleItem back to the fragment...
                ExampleItem data = items.get(position);
                listener.onTextClick(data);
            }
        });
    }
}

Implement the fragment with the interface:

class YourFragment extends Fragment implements OnTextClickListener {

    public onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setAdapter(new ExampleAdapter(this));
    }

    void onTextClick(ExampleItem data) {
        // Now you can do however you want with the data here...
        Toast.makeText(getActivity(), "Got: " + data, Toast.LENGTH_SHORT).show()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...