Recyclerview项目在滚动时会自动单击

时间:2018-12-23 06:34:13

标签: android android-recyclerview

当我滚动recyclerview时。其中的项目会自动被点击。如何解决此问题。

这是我的recyclerview(OnItemTouchListner)。在自动滚动项目时 被点击

recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
                @Override
                public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
                    View view=rv.findChildViewUnder(e.getX(),e.getY());
                    String profile=null;
                    if(view!=null){
                        int position=rv.getChildAdapterPosition(view);

                        try {
                            JSONObject jsonObject=new JSONObject(arrayList.get(position));
                            profile=jsonObject.getString("profile");
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        Intent intent=new Intent(getActivity(),ProfileActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        Bundle bundle=new Bundle();
                        bundle.putInt("position",position+1);
                        bundle.putString("profile",profile);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                    return false;
                }

                @Override
                public void onTouchEvent(RecyclerView rv, MotionEvent e) {

                }

                @Override
                public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

                }
            });

1 个答案:

答案 0 :(得分:0)

您可以使用

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

            try {
                        JSONObject jsonObject=new JSONObject(arrayList.get(position));
                        profile=jsonObject.getString("profile");
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                    Intent intent=new Intent(getActivity(),ProfileActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    Bundle bundle=new Bundle();
                    bundle.putInt("position",position+1);
                    bundle.putString("profile",profile);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);

        }
    });