Handling RecyclerView OnClickListener in Activity / Fragment

When going from ListView

to, RecyclerView

handling "onItemClick" seems painful.

One of the two most popular approaches is to do ViewHolder

ViewHolder

View.OnClickListener

and process onClick

there. An example of this approach is shown here.

What's a good practice to trigger this "onClick" from the element ViewHolder

in the Fragment / Activity where it resides RecycleView

and handle it there?

Thank!

+3


source to share


3 answers


As you said, there are several approaches and the implementation depends on the design of the rest of the code, what the logic is and how you want the objects to be.

  • Add an onItemClickListener for the entire owner can be found here and its a good way to do it if you only have a click on the entire item. Please note, if you are going to implement swipe2dismiss with it, it will lead to some pain due to touch events and multiple TouchEventListeners
  • Let your fragment implement OnClickListener and pass it to the adapter, then pass it to the holder so that it receives the onClick of the view events you want, then create it to handle it.


IMO: the best practice is to pass OnClickListeners, because when you want to use this view in another class and want to use a different click handling, it will be easier to implement.

+2


source


This code actually works for me. Place it in your Activity class in a method oncreate()

.



final GestureDetector mGestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return true;
    }

});

recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
        View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());


        if (child != null && mGestureDetector.onTouchEvent(motionEvent)) {
            drawerLayout.closeDrawers();
            //  Toast.makeText(MainActivity.this, "The Item Clicked is: " + recyclerView.getChildPosition(child), Toast.LENGTH_SHORT).show();
            // below code get the position of data
            int a=recyclerView.getChildPosition(child);


            return true;

        }

        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
});

      

+1


source


You can create an onclicklistener inside the viewer and implement it in action here is a demo: https://github.com/yefengfreedom/YfRecyclerView

and the code below:

/**
 * click recycler view item
 *
 * @param <T> t
 */
public interface OnItemClickListener<T> {
    void onItemClick(View view, T t);
}

      

.........

public void setOnItemClickListener(RecyclerViewInterface.OnItemClickListener onItemClickListener) {
    mOnItemClickListener = onItemClickListener;
}

      

0


source







All Articles