I am able to detect the individual recycler view item position and able to toast it on clicked. Now I wan't to move on to a new activity and show the details of the item clicked, how can I do that? Say, I am displaying Contact names list and onclick I wan't to open a new activity show that contact's details... At least, how can I toast the contact name again on clicking on that contact item, how are the current variables on clicking available to me? I am planning to bundle those variables and send them with intent and display there.
i know i have to implement here
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position){
//i know i have to implement here
}
}
I had the same problemm untill i did this.
Created a custome RecyclerListener:
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
then in the activity with the recyclerView:
private void registerCallClickBack() {
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity().getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("contact_name", customList.get(position).getName());
intent.putExtra("contact_image", customList.get(position).getImage());
intent.putExtra("contact_tel", customList.get(position).getMobile());
intent.putExtra("contact_email", customList.get(position).getEmail());
startActivity(intent);
}
}));
}
where customList is my ArrayList of contacts.
Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With