RecyclerView onClick for multiple buttons and handling from Activity

user2695433 picture user2695433 · May 29, 2017 · Viewed 27.4k times · Source

I'm using RecyclerView with CardView and inside the CardView have 2 buttons. Now, have implemented the onClick events by implementing View.OnClickListener by the ViewHolder static class and overriding the event (its working )like

    @Override
    public void onClick(View v) {

        if (v.getId() == mClassBtn.getId()){

        }
        else if (v.getId() == mDaysBtn.getId()){

        }
     }

Looking for a solution/pattern to handle the Click in the Activity class like

adapter = new ItemsListAdapter(getActivity(), data, new CustomItemClickListener() { 

    @override
    public void onItemClick(View v, int position) {

       Log.d(TAG, "clicked position:" + position);
       // which button clicked and separate action for each button
    } 
});
// CustomItemClickListener -- interface in the adapter class which holds the click event .

The above code working fine and able to log the Clicks but not able to differentiate which button has clicked.

Appreciate any help

Answer

headshoT picture headshoT · May 29, 2017

here is the best way to achieve that:--

this is how i have implemented

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

    private List<GetUserFollowDetailsResponseData> followersList;
    Context context;
    public FollowersDetailsAdapterListener onClickListener;


    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView rv_tv_follower_name, rv_tv_follower_desig;
        public CircleImageView rv_iv_follower_image;
        RelativeLayout rv_rel_layout_followers, rel_layout_followers;
        LikeButton rv_iv_follower_icon;

        public MyViewHolder(View view) {
            super(view);
            rv_rel_layout_followers = (RelativeLayout) view.findViewById(R.id.rv_rel_layout_followers);
            rel_layout_followers = (RelativeLayout) view.findViewById(R.id.rel_layout_followers);
            rv_iv_follower_image = (CircleImageView) view.findViewById(R.id.rv_iv_follower_image);
            rv_tv_follower_name = (TextView) view.findViewById(R.id.rv_tv_follower_name);
            rv_tv_follower_desig = (TextView) view.findViewById(R.id.rv_tv_follower_desig);
            rv_iv_follower_icon = (LikeButton) view.findViewById(R.id.rv_iv_follower_icon);
            rel_layout_followers.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.iconTextViewOnClick(v, getAdapterPosition());
                }
            });
        }

    }

    public FollowersDetailsAdapter(List<GetUserFollowDetailsResponseData> FollowersList, FollowersDetailsAdapterListener listener) {
        this.followersList = FollowersList;
        this.onClickListener = listener;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.followers_list_row, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if (followersList.get(position).getImage() != null && !followersList.get(position).getImage().trim().equalsIgnoreCase("")) {
            Picasso.with(context)

                    .load(followersList.get(position).getImage())
                    .resize(200, 200).into(holder.rv_iv_follower_image);
        }

        holder.rv_tv_follower_name.setText(followersList.get(position).getName());
        holder.rv_tv_follower_desig.setText(followersList.get(position).getDescription());
        if (followersList.get(holder.getAdapterPosition()).getID() == Integer.parseInt(ApplicationPreference.getUserId())) {
            holder.rv_iv_follower_icon.setVisibility(View.GONE);
        } else {
            holder.rv_iv_follower_icon.setVisibility(View.VISIBLE);
            if (followersList.get(position).getIsFollowing() == 0) {
                holder.rv_iv_follower_icon.setLiked(false);
            } else if (followersList.get(position).getIsFollowing() == 1) {
                holder.rv_iv_follower_icon.setLiked(true);
            }


        }
        holder.rv_iv_follower_icon.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {

                onClickListener.iconImageViewOnClick(likeButton, position);
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                onClickListener.iconImageUnFollowOnClick(likeButton, position);
            }
        });

    }

    public interface FollowersDetailsAdapterListener {

        void iconTextViewOnClick(View v, int position);

        void iconImageViewOnClick(View v, int position);

        void iconImageUnFollowOnClick(View v, int position);
    }

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

and in your activity where you initialize the adapter--

 followersDetailsAdapter = new FollowersDetailsAdapter(followersList, new FollowersDetailsAdapter.FollowersDetailsAdapterListener() {
            @Override
            public void iconTextViewOnClick(View v, int position) {
                gotoUserProfile(followersList.get(position).getID());

            }

            @Override
            public void iconImageViewOnClick(View v, int position) {

                followUserProfile(followersList.get(position).getID(),1);
            }

            @Override
            public void iconImageUnFollowOnClick(View v, int position){

                followUserProfile(followersList.get(position).getID(),0);
            }
        });