حسین اچ
حسین اچ

Reputation: 107

How to add an Item Click Listener in `RecyclerView.Adapter' using CardView Item

How can I add an Item Click Listener for my `RecyclerView.Adapter' when the user clicks on the Card View item, Data sent to the PostContent Fragment?

Also, is it possible to send the data from this adapter to the new fragment using intent?

Please note my code:

public class PostDataAdapter extends RecyclerView.Adapter<PostDataAdapter.MyViewHolder> {

    private List<PostData> PostDataList ;
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView vPostContent, vPostDate, vPostAuthor, vPostTitr,VPostLikes,VPostViews;
        public ImageView vPostPhoto;

        public MyViewHolder(View v) {
            super(v);

            vPostContent = v.findViewById(R.id.PostContentTv);
            vPostDate = v.findViewById(R.id.PostDateTv);
            vPostAuthor = v.findViewById(R.id.PostAuthorTv);
            vPostTitr = v.findViewById(R.id.PostTitrTv);
            vPostPhoto = v.findViewById(R.id.PostPhoto);
            VPostLikes=v.findViewById(R.id.PostLikeTv);
            VPostViews=v.findViewById(R.id.PostViewTv);


        }

    }

    public PostDataAdapter(List<PostData> postDataList) {
        PostDataList = postDataList;
    }

    @Override
    public PostDataAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_posts, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.vPostDate.setText(PostDataList.get(position).getPostDate());
        holder.vPostTitr.setText(PostDataList.get(position).getPostTitr());
        holder.vPostContent.setText(PostDataList.get(position).getPostContent());
        holder.vPostAuthor.setText(PostDataList.get(position).getPostAuthor());
        holder.VPostViews.setText(PostDataList.get(position).getPostViews());
        holder.VPostLikes.setText(PostDataList.get(position).getPostLikes());
        new DownloadImageTask(holder.vPostPhoto).execute(PostDataList.get(position).getImgpost());

    }

    @Override
    public int getItemCount() {
        return PostDataList.size();
    }
}

Upvotes: 0

Views: 435

Answers (4)

Fahime Ghasemi
Fahime Ghasemi

Reputation: 1035

for adding click item for a Cardview, you can find the Cardview in MyViewHolder class by id and in onBindViewHolder set a click listerner for it like the following

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

holder.vPostCardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //write your codes here

    }
});
}

if you have an intent that you want to send it's data to a fragment, you can get the intent data and send them with bundle to your fragment. for example do something like the following.

Bundle bundle = new Bundle();
bundle.putString("your_key",intent.getStringExtra("your_item_key_in_intent"));

and after that send bundle to your fragment with

fragment.setArguments(bundle);

Upvotes: 0

Nikhil Bansal
Nikhil Bansal

Reputation: 187

To add a ItemCLickListener for RecyclerView, you need to implement a custom Interface which the Fragment will implement. When the list item is clicked, then the callback function of the interface is called.

CustomItemClickListener.java:

public CustomItemClickListener {
    void onItemClick(Object data);
}

Just add these to the PostDataAdapter:

PostDataAdapter.java:

private CustomItemClickListner clickListener;
public PostDataAdapter(CustomItemClickListner listener, List<PostData> postDataList) {
    PostDataList = postDataList;
    clickListener = listener
}


@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.vPostCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Modify the parameters of the function according to what you want to send to the fragment 
            // As soon as this is called, the `onItemClick` function implemented in the Fragment gets called.
            clickListener.onItemClick(Object data);
        }
    });
}

Fragment.java:

CustomFragment extends Fragment implements CustomItemClickListener {
    public CustomFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view;
        PostDataAdapter adapter = new PostDataAdapter(this, new ArrayList<PostData>)
        return view;
    }

    @Override
    public void onItemClick(Object data) {
        // Handle the data sent by the adapter on item click
    }
}

Upvotes: 1

user4260853
user4260853

Reputation:

Intents can be used to send data to new activities but not fragments You'd have to use the Fragment Manager and attach a bundle to it to send data. You can refer to the documentation here on how to do so:

https://developer.android.com/training/basics/fragments/communicating#Deliver

To handle click on cards, you can create a listener when you create PostDataAdapter. Refer to the following link for a simple example:

https://stackoverflow.com/a/40584425/4260853

Upvotes: 0

mnp343
mnp343

Reputation: 329

There are two ways you can do this.

  1. Write recyclerview.onitem touchlistener(...). Then consume that event in your fragment. As you will get item position inside touchlistener callback, you can take out data from your list directly from the list you passed to your adapter (Assuming you have list reference outside in your fragment.)

  2. Oobserver pattern. Define a functional interface (one callback method with required parameters of the data you want to pass) implement inside your fragment. Send its reference with the constructor of adapter. Then Store reference in a interface type variable inside adapter. Write click listener on card. And on the card click, invoke method using interface type variable.

Upvotes: 0

Related Questions