Closed kyungin-park closed 7 years ago
I found a workaround way.
On StickyHeaderDecoration.java
I added the function "isViewClicked" under findHeaderViewUnder.
public View findHeaderViewUnder(float x, float y) {
.....
}
/**
*
* @param x : getX
* @param y : getY
* @param viewId : resource id of view you clicked.
* @return
*/
public boolean isViewClicked(float x, float y, int viewId) {
for (RecyclerView.ViewHolder holder : mHeaderCache.values()) {
final View itemView = findHeaderViewUnder(x, y);
if (itemView == null) {
return false;
}
final float translationX = ViewCompat.getTranslationX(itemView);
final float translationY = ViewCompat.getTranslationY(itemView);
final View child = itemView.findViewById(viewId);
if (child == null) {
return false;
}
if (x >= child.getLeft() + translationX &&
x <= child.getRight() + translationX &&
y >= child.getTop() + translationY &&
y <= child.getBottom() + translationY) {
return true;
}
}
return false;
}
On Fragment file,
I added item touch listner by insparation of original code,
final StickyHeaderDecoration decor = new StickyHeaderDecoration(adapter);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(decor, 1);
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
// only use the "UP" motion event, discard all others
if (e.getAction() == MotionEvent.ACTION_UP) {
// find the view on the header that was clicked
if (decor.isViewClicked(e.getX(), e.getY(), R.id.btn)) { <--- here can check if you clicked the button.
// do what you want. It works very well.
// Toast.... etc...
return true;
}
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
// ignore
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
// ignore
}
});
Duplicate of #38
I was trying to add some buttons on the header view. And I realized that it doesn't allow to be clicked because the view was drew by onDrawOver. It is not the normal item view on the RecyclerView but just the same shape of the item view. Could you recommend how I can add the click event on the header buttons?
Someone asked the question on the StackOverFlow page. But still I don't have any solution from it. http://stackoverflow.com/questions/31102738/how-to-handle-click-event-in-recyclerview-itemdecoration