In our app we have list of messages. We are switching to RecyclerView
.
Our messages can consist from texts, images.
Sample message can look like this.
To handle clicks use this class:
public class RecyclerViewItemClickListener implement RecyclerView.OnItemTouchListener {
public static interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onItemLongClick(View view, int position);
}
private OnItemClickListener mListener;
private GestureDetector mGestureDetector;
public RecyclerViewItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e)
{
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if(childView != null && mListener != null)
{
mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
}
}
});
}
@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) {
}
}
and we implement this clicks in activity like this:
chatView.setLongClickable(true);
chatView.addOnItemTouchListener(
new RecyclerViewItemClickListener(this, chatView, new RecyclerViewItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
//handleSingleClick(view, position);
}
@Override
public void onItemLongClick(View view, int position)
{
handleLongPress(position);
}
})
);
Clicks for message is working, now I want also handle userAvatar
clicks from same GestureDetection
. Is it possible to detect which list item layout was clicked from GestureDetector
?
In other words, can I get GestureDetecter
clicked child. In my case click is set to whole item(avatar, text message, image). Can I detect which element inside this item was clicked(for example I want catch avatar click).
Android RecyclerView Multiple ViewType Project Structure We'll be implementing three view types (text, image, audio) that are inflated by three different layouts. Each has its own implementation specified in the adapter class.
The major advantage of the usage of RecyclerView is the performance when loading list items in the list to the view. RecyclerView prepares the view behind and ahead beyond the visible entries. It gives significant performance when you need to fetch the bitmap image in your list from a background task.
Handle the click in the adapter of your RecyclerView like below example:
private class BookmarkViewHolder extends RecyclerView.ViewHolder {
protected ImageView thumbnailIcon;
protected TextView postTitle;
protected TextView postViewCount;
public BookmarkViewHolder(View v) {
super(v);
thumbnailIcon = (ImageView) v.findViewById(R.id.thumbnailIcon);
postTitle = (TextView) v.findViewById(R.id.postTitleTV);
postViewCount = (TextView) v.findViewById(R.id.viewCountTV);
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
thumbnailIcon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
postTitle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
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