Reputation: 11
I have a recyclerview that has image, texts & image button, when I use the RecyclertouchListener and add onItemTouchListener it overclicks the button while the button has onclicklistener
If I don't handle clicklistener for the recyclerview, the button works fine. Only when the recyclerview has something to do in the onItemtouchListener.
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
private GestureDetector gestureDetector;
ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener)
{
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener()
{
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if(child != null && clickListener != null)
clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if(child != null && clickListener != null && gestureDetector.onTouchEvent(e))
clickListener.onClick(child, rv.getChildAdapterPosition(child));
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
startActivity(new Intent(getActivity(), Show.class).putExtra("show", data.get(position)));
}
@Override
public void onLongClick(View view, int position) {
}
}));
Upvotes: 0
Views: 2140
Reputation: 11
So for the RecyclerTouchListener there is no way I found to change the focus from the recyclerview to the button. As the onInterceptTouchEvent steals the focus of the viewgroup to perform the onclick, which means it steals the whole focus on the touch to make its child perform the click which is the recyclerview here. So anything inside the recyclerview can't intercept.
For more explanation try to search on how the onInterceptTouchEvent works from passing false when you tap on the window then false for the viewgroup then the recyclerview which calls the viewgroup onInterceptTouchEvent to apply the onclick. Also I could't adjust the scale of the child (recyclerview).
So I found a solution for the second way to implement onclick for the itemview in the recycleradapter class & I made it could be called from outside the adapter.
Thanks Tara for inspiring a little. Here's is the reference [http://alexzh.com/tutorials/recyclerview-click-listener/][1]
Her's the code I modified
1- Add Interface for the click
public interface ClickListener {
public void onClick (View view, int position);
public void onLongClick (View view, int position);
}
2- In Class RecyclerAdapter add method setClickListener
public void setClickListener(ClickListener _clickListener) {
this.clickListener = _clickListener;
}
3- Call the clickListener.onClick & onLongClick in onBindViewHolder
@Override
public void onBindViewHolder(final HawkViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickListener.onClick(view, holder.getPosition());
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
clickListener.onLongClick(view, holder.getPosition());
return true;
}
});
4- Call the method recyclerAdapter.setClickListener in the activity
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerAdapter = new CustomRecyclerAdapter(this);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.setClickListener(new ClickListener() {
@Override
public void onClick(View view, int position) {
}
@Override
public void onLongClick(View view, int position) {
}
});
Upvotes: 1
Reputation: 2648
Add onClick listener in RecyclerAdapter some thing like this. You can take help from this
final int position=i;
ListRowHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"Recycler Click"+position+myTitle,Toast.LENGTH_SHORT).show();
Intent intent=new Intent(mContext,ItemDetails.class);
intent.putExtra("itemName",myTitle);
mContext.startActivity(intent);
}
});
Upvotes: 0