How to add Onclick listener to recycler view
You can handle this using two ways
1). Gesture touch https://www.google.co.in/amp/sapandiwakar.in/recycler-view-item-click-handler/amp/
2).Using interface in adapter https://antonioleiva.com/recyclerview-listener/
I suggest second way using interface
How to use interface for recycleritemclick
public class RecycleViewAdapter extends
RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
private List<FileName> fileNames;
private Context context;
//declare interface
private OnItemClicked onClick;
//make interface like this
public interface OnItemClicked {
void onItemClick(int position);
}
public RecycleViewAdapter(Context context,List<FileName> fileNames) {
this.context = context;
this.fileNames = fileNames;
}
Now assign click to interface
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
//............//
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.onItemClick(position);
}
});
}
At the end of the adapter class up from finishing bracket make one method to assign itemclick to interface
public void setOnClick(OnItemClicked onClick){
this.onClick=onClick;
}
In MainActivity.java Bind the item click with adapter
public class MainActivity extends Activity implements OnItemClicked {
private RecyclerView mRecyclerView;
private CityAdapter mAdapter;
private List<City> cities;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city);
mRecyclerView = (RecyclerView)findViewById(R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new CityAdapter(cities, R.layout.row_city, this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnClick(MainActivity.this); // Bind the listener
}
@Override
public void onItemClick(int position) {
// The onClick implementation of the RecyclerView item click
//ur intent code here
}
}
For Kotlin code please refer How to handle recyclerview item click in kotlin?
More queries? comment.
First add this class to your package
public class MyTouchListener implements RecyclerView.OnItemTouchListener {
/*Change these as per your need*/
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private static final int SWIPE_MAX_OFF_PATH = 250;
private OnTouchActionListener mOnTouchActionListener;
private GestureDetectorCompat mGestureDetector;
public static interface OnTouchActionListener {
public void onLeftSwipe(View view, int position);
public void onRightSwipe(View view, int position);
public void onClick(View view, int position);
}
public MyTouchListener(Context context, final RecyclerView recyclerView,
OnTouchActionListener onTouchActionListener){
mOnTouchActionListener = onTouchActionListener;
mGestureDetector = new GestureDetectorCompat(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// Find the item view that was swiped based on the coordinates
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
int childPosition = recyclerView.getChildPosition(child);
mOnTouchActionListener.onClick(child, childPosition);
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// Find the item view that was swiped based on the coordinates
View child = recyclerView.findChildViewUnder(e1.getX(), e1.getY());
int childPosition = recyclerView.getChildPosition(child);
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (mOnTouchActionListener != null && child != null) {
mOnTouchActionListener.onLeftSwipe(child, childPosition);
}
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (mOnTouchActionListener != null && child != null) {
mOnTouchActionListener.onRightSwipe(child, childPosition);
}
}
} catch (Exception e) {
// nothing
}
return false;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetector.onTouchEvent(e);
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
// do nothing
}}
Then addOnItemTouchListener like this:
myRecyclerView.addOnItemTouchListener(new MyTouchListener(mContext,
myRecyclerView,
new MyTouchListener.OnTouchActionListener() {
@Override
public void onLeftSwipe(View view, int position) {//code as per your need
}
@Override
public void onRightSwipe(View view, int position) {//code as per your need
}
@Override
public void onClick(View view, int position) {//code as per your need
}
}));
Change your adapter to this
public class RecycleViewAdapter extends
RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
private List<FileName> fileNames;
private Context context;
private OnItemClicked listener;
public RecycleViewAdapter(Context context, List<FileName> fileNames, OnItemClicked listener) {
this.context = context;
this.fileNames = fileNames;
this.listener = listener;
}
public BookingHistoryFragment() {
// Required empty public constructor
}
@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
ViewGroup mainGroup = (ViewGroup) mInflater.inflate(
R.layout.grid_item, parent, false);
RecycleViewHolder listHolder = new RecycleViewHolder(mainGroup);
return listHolder;
}
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
final FileName model = fileNames.get(position);
RecycleViewHolder mainHolder = (RecycleViewHolder) holder;// holder
mainHolder.title.setText(model.getName());
mainHolder.note.setText(model.getShorttext());
// Add click listener for root view
view.getRootView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onItemClick(view, position)
}
});
}
@Override
public int getItemCount() {
return (null != fileNames ? fileNames.size() : 0);
}
public interface OnItemClicked {
void onItemClick(View view, int position);
}
}
Now pass ItemClickInterface to the adapter constructor while initializing from activity or fragment. Instead of:
RecycleViewAdapter adapter = new RecycleViewAdapter(this, filenames);
Use this:
RecycleViewAdapter adapter = new RecycleViewAdapter(this, filenames, new RecycleViewAdapter.OnItemClicked () {
@Override
public void onItemClick(View view, Position position) {
}
});
Simplest solution would be to access the "itemView" public variable of holder class and set onClickListener on this.
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, NewActivity.class);
intent.putExtra("FileName", list.get(position));
context.startActivity(intent);
}
});
}