Implement Click Listener on RecyclerView


  • In this post Implement click listener on RecyclerView
  • I put the code after the RecyclerView setAdapter for click listener on RecyclerView following :-

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 rv, MotionEvent e) {
        View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
        if (child != null && mGestureDetector.onTouchEvent(e)) {

            int a=recyclerView.getChildPosition(child);
            Toast.makeText(MainActivity.this, fruit[a], Toast.LENGTH_SHORT).show();
            }
        return true;
    }
    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }
    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
});


  • After that You run the application it show that name clicked by you.
  • Output Shows like that





  • I hope it is usesful for Android lerner's with ease.....

Comments

AddThis