双击Recycler View项的addOnItemTouchListener

时间:2017-05-04 04:55:53

标签: android android-recyclerview

在recycler视图中,我有多个项目点击监听器,我使用相同的适配器作为多个片段

recyclerView.addOnItemTouchListener(
            new MyRecyclerItemClickListener(getActivity(), new MyRecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View views, final int position) {
                    //Values are passing to activity & to fragment as well
                    view = views;
                    share_txt = (TextView) view.findViewById(R.id.share_txt);
                    complaint_id_txt = (TextView) view.findViewById(R.id.complaint_id_txt);
                    comment_txt = (TextView) view.findViewById(R.id.comment_txt);
                    like_txt = (TextView) view.findViewById(R.id.like_count);
                    like_id_txt = (TextView) view.findViewById(R.id.like_id_txt);
                    like_img = (ImageView) view.findViewById(R.id.lik_img);
                    complaint_imgview = (ImageView) view.findViewById(R.id.complaint_img);

                    complaint_id = complaint_id_txt.getText().toString();
                    final String like_str = like_id_txt.getText().toString();


                    comment_txt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getActivity(), Activity_Comment.class);
                            startActivity(intent);
                        }
                    });

                    complaint_imgview.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            complaintAssign(position);

                            complaint_id = complaintList.get(position).getComplaintId();

                            Intent intent = new Intent(getActivity(), Activity_ComplaintDetail.class);
                            intent.putExtra("position", position);
                            startActivity(intent);
                        }
                    });

                    like_txt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            if (like_str.equalsIgnoreCase("Liked")) {

                                Toast.makeText(context, "You have already liked", Toast.LENGTH_SHORT).show();

                            } else if (like_str.equalsIgnoreCase("Like")) {
                                like_txt.setTextColor(ContextCompat.getColor(context, R.color.navy));
                                like_img.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.like));
                                lik = likeJson();
                                if (lik == 2323) {
                                    like_txt.setTextColor(ContextCompat.getColor(context, R.color.black));
                                    like_img.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.unlike));
                                } else {

                                }


                            }

                        }
                    });
                    like_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            if (like_str.equalsIgnoreCase("Liked")) {

                                Toast.makeText(context, "You have already liked", Toast.LENGTH_SHORT).show();

                            } else if (like_str.equalsIgnoreCase("Like")) {
                                like_txt.setTextColor(ContextCompat.getColor(context, R.color.navy));
                                like_img.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.like));
                                lik = likeJson();
                                if (lik == 2323) {
                                    like_txt.setTextColor(ContextCompat.getColor(context, R.color.black));
                                    like_img.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.unlike));
                                } else {

                                }

                            }

                        }
                    });

                }

                public void onLongClick(View view, int position) {
                    Toast.makeText(getActivity(), "Select Category",
                            Toast.LENGTH_LONG).show();
                }
            })
    );

它的工作正常,但它仅适用于第二次点击第一次点击它不会做任何操作。 recyclerView.setFocusable(假);和recyclerView.setFocusableInTouchMode(false);但它对我来说不会有用,任何人都可以帮助我

提前致谢。

1 个答案:

答案 0 :(得分:1)

您只有在点击了recyclerView元素后才设置了complaints_img的监听器,因此您必须单击两次才能执行任何操作。将complaint_img.setOnClickListener()移动到recyclerView的适配器。