RecyclerView onClick for multiple buttons and handling from Activity

后端 未结 5 680
深忆病人
深忆病人 2021-01-31 22:01

I\'m using RecyclerView with CardView and inside the CardView have 2 buttons. Now, have implemented the onClick events by imp

相关标签:
5条回答
  • 2021-01-31 22:42

    Here my solution if it could help someone.

    You could click in one item of the recyclerview or one of the two buttons.

    The interface implements:

    onStudentItemClick, for click in the recyclerview item

    onChatItemClick and onAbsenceItemClick for click the buttons

    With

     holder.imgChat.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnClickListener.onChatItemClick(mStudents.get(index));
    
                    }
                });
    

    I set the click listener for one button

    And in the class StudentViewHolder I implement OnClickListener and override onClik

    @Override
            public void onClick(View v) {
    
                if (mOnClickListener != null){
                    int clickedPosition = getAdapterPosition();
                    mOnClickListener.onStudentItemClick(mStudents.get(clickedPosition));
                }
            }
    

    Here the Adapter:

    public class StudentsAdapter extends RecyclerView.Adapter<StudentsAdapter.StudentsViewHolder> {
    
        private ListItemClickListener mOnClickListener;
        private ArrayList<Student> mStudents;
    
        public interface ListItemClickListener {
            void onStudentItemClick(Student student);
            void onChatItemClick(Student student);
            void onAbsenceItemClick(Student student);
        }
    
    
        public StudentsAdapter(ArrayList<Student> students, ListItemClickListener listener) {
            mStudents = students;
            mOnClickListener = listener;
        }
    
        @Override
        public StudentsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            Context context = viewGroup.getContext();
            int layoutIdForListItem = R.layout.student_list_item;
            LayoutInflater inflater = LayoutInflater.from(context);
            boolean shouldAttachToParentImmediately = false;
    
            View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
            StudentsViewHolder viewHolder = new StudentsViewHolder(view);
    
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(StudentsViewHolder holder, int position) {
            final Integer index = position;
            holder.tvStudent.setText(mStudents.get(position).nameForList());
            holder.imgChat.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnClickListener.onChatItemClick(mStudents.get(index));
    
                }
            });
            holder.imgAbsence.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnClickListener.onAbsenceItemClick(mStudents.get(index));
    
                }
            });
    
        }
    
        public Student getItem(Integer position){
            return mStudents.get(position);
        }
    
    
        @Override
        public int getItemCount() {
            return mStudents.size();
        }
    
    
        class StudentsViewHolder extends RecyclerView.ViewHolder
                implements OnClickListener {
    
            ImageView imgStudent;
            TextView tvStudent;
            ImageButton imgChat, imgAbsence;
    
            public StudentsViewHolder(View itemView) {
                super(itemView);
    
                imgStudent = (ImageView) itemView.findViewById(R.id.img_student);
                tvStudent = (TextView) itemView.findViewById(R.id.tv_student);
                imgChat = (ImageButton) itemView.findViewById(R.id.btn_chat_student);
                imgAbsence = (ImageButton) itemView.findViewById(R.id.btn_absence);
    
                itemView.setOnClickListener(this);
            }
    
    
            @Override
            public void onClick(View v) {
    
                if (mOnClickListener != null){
                    int clickedPosition = getAdapterPosition();
                    mOnClickListener.onStudentItemClick(mStudents.get(clickedPosition));
                }
            }
        }
    }
    

    Fragment the implement the interface:

    public class SubjectDetailFragment extends Fragment implements  StudentsAdapter.ListItemClickListener {
    ...
    @Override
    public void onStudentItemClick(Student student) {
        StudentDetailFragment detailFragment = StudentDetailFragment.newInstance();
        Bundle bundle = new Bundle();
        bundle.putParcelable("student", student);
        detailFragment.setArguments(bundle);
    
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frame_layout, detailFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    
    }
    
    @Override
    public void onChatItemClick(Student student) {
        ...
    }
    
    @Override
    public void onAbsenceItemClick(Student student) {
        ...
    
    }
    
    0 讨论(0)
  • 2021-01-31 22:44

    I do it this way.

    1. Set an OnClickListener on your Button in the ViewHolder

    2. Create a method in your activity with the button position as a parameter

    3. Pass the position of the clicked entry and do stuff

    ViewHolder

    yourButton.setOnClickListener(new View.OnClickListener()
    {
         @Override
         public void onClick(View v) 
         {
    
            int position = getAdapterPosition(); //Clicked entry in your List
    
            //Method from your activity
            yourActivity.methodOnBtnClick(position);
         }
    

    Activity

    public void methodOnBtnClick(int position)
    {
        yourList.remove(position);
        recycler.getAdapter().notifyDataSetChanged();
    }
    
    0 讨论(0)
  • 2021-01-31 22:47

    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);
                }
            }); 
    
    0 讨论(0)
  • 2021-01-31 22:48

    Please, try this principle to create a communication between activity, adapter through the interface, I hope this principle helps you:

    Model Class:

    public class YourModel {
    
        private String firstName;
        private String lastName;
    
        public YourModel(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        //... setters & getters
    }
    

    Interface:

    public interface AdapterCallback {
        void onClickCallback(YourModel itemModel);
    }
    

    Adapter:

    public class YourAdapter extends RecyclerView.Adapter<YourAdapter.ViewHolder> {
    
        private List<YourModel> data;
        private AdapterCallback callback;
        private Context context;
    
        public YourAdapter(List<YourModel> data, AdapterCallback callback) {
            this.data = data;
            this.callback = callback
        }
    
        @Override
        public YourAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            context = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);
            View item = inflater.inflate(R.layout.item_recycler, parent, false);
            return new ViewHolder(item);
        }
    
        @Override
        public void onBindViewHolder(YourAdapter.ViewHolder holder, int position) {
            YourModel itemModel = data.get(position);
    
            String lastName  = itemModel.getLastName();  
            String firstName = itemModel.getFirstName();
    
            holder.tvFirstName.setText(firstName);
            holder.tvLastName.setText(lastName);
    
            holder.tvFirstName.setOnClickListener { 
               callback.onClickCallback(itemModel));
            }
        }
    
        @Override
        public int getItemCount() {
            return data.size();
        }
    
        static class ViewHolder extends RecyclerView.ViewHolder {
    
            private TextView tvFirstName;
            private TextView tvLastName;
    
            ViewHolder(View itemView) {
                super(itemView);
                tvFirstName = (TextView) itemView.findViewById(R.id.tv_first);
                tvLastName  = (TextView) itemView.findViewById(R.id.tv_last);
            }
    
    }
    

    Activity:

    public class MyActivity extends AppCompatActivity implements AdapterCallback {
    
    private YourAdapter adapter;
    private List<YourModel> items;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 1. init your list && fill it with needed data
        // 2. init your adapter 
        // 3. put your list as parameter into constructor + interface
        items = new ArrayList<YourModel>()
        items.add(YourModel("John", "Doe"));
        items.add(YourModel("Bob", "Marley"));
        adapter = new YourAdapter(items, this)
    }
    
    @Override
    public void onClickCallback(YourModel itemModel) {
        Toast.makeText(this, itemModel.getFirstName(), Toast.LENGTH_SHORT).show();
    }
    

    I think that this information is enough to understand what I mean. The same principle you can create multiple OnClick methods in your interface or put one onClick for a few buttons and check id or something else to get needed information that you need. Good luck

    0 讨论(0)
  • 2021-01-31 23:03

    Try this

    In your activity do like this

    CustomItemClickListener listner;
    
    listner=YourActivityName.this   // in OnCreate Method
    

    and pass this listner in your adapter

    adapter = new ItemsListAdapter(getActivity(), data, listner);
    

    And in your Adapter use it like this

    CustomItemClickListener listner;
    
    this.listner=listner; // In your Adapter COnstructor
    

    and last in your onBindViewHolder do like this

    holder.button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listner.onItemClick(holder.getLayoutPosition()); //getting position
                }
            });
    
    holder.button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listner.onItemClick(holder.getLayoutPosition()); //getting position
                }
            });
    
    0 讨论(0)
提交回复
热议问题