RecyclerView item internal click event, item click event conflict

1. There is no click event in the recyclerview item internal control
In the case that there is no click event in the internal control of recyclerview item. Item set click event can be set by adding touch event

recyclerview.addOnItemTouchListener();//Add touch event monitoring for item

OnItemTouchListener Corresponding definition of

/**Processing class*/
 private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener {

        public  boolean onSingleTapUp(MotionEvent event){
            View child = recyclerView.findChildViewUnder(event.getX(), event.getY());
            if (child != null){
                RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(child);
                onItemClick(viewHolder);
            }
            return true;
        }

        public  void onLongPress(MotionEvent event){
            View child = recyclerView.findChildViewUnder(event.getX(), event.getY());
            if (child != null){
                RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(child);
                onItemLongClick(viewHolder);
            }
        }



    }
/**Implementation of general recyclerView item click event*/
public abstract MyItemClickListener implments RecylerView.OnItemTouchListener{
    //view
    private final RecyclerView recyclerView;
    //Gesture recognition class
    private final GestureDetectorCompat mGestureDetector;

    public MyItemClickListener(RecyclerView recyclerView){
         this.recyclerView = recyclerView;
        mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(),new ItemTouchHelperGestureListener());
    }
      @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        mGestureDetector.onTouchEvent(e);
    }

      @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        mGestureDetector.onTouchEvent(e);
        return false;
    }

     @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }

    //Write the above class as an inner class and call the method below.
    public abstract void onItemClick(RecyclerView.ViewHolder viewHolder);

    public abstract void onItemLongClick(RecyclerView.ViewHolder viewHolder);
}
//This method is only applicable to item internal control no listening event.

2.item internal control click event
In this case, there are listening events for internal item s

In the adapter, click event and long press event are implemented through click event and long press event.
//The listener is convenient to set call click and long press events to be written directly in a listening class
public interface MyItemClickListener{
    void onItemClick();
    void onItemLongClick();
}
//Adapter
MyItemClickListener listener=null;
onCreateViewHolder() Click and long press events are set in.
v.setOnClickListener(this);
v.setOnLongClickListener(this);

onBindViewHolder()In the method
//Set the tag for view;
holder.itemView.setTag(position);

//Call method settings
public void setOnItemListener(MyItemClickListener listener){
    this.listener=listener;
}

@Override
public void onClick(View v) {
    if(listener!=null){
        listener.onItemClick
    }
}

//The event is called directly after the adapter instance. In the corresponding placetry{}catch(Exception e){}

The above is the solution to the problem of event delivery encountered in the actual coding.

Posted on Sun, 03 May 2020 17:45:24 -0400 by amorphous