[英]Android: How to bring recyclerview scrollbar to front over RecyclerView.ItemDecoration?
[英]How to handle click-event in RecyclerView.ItemDecoration?
我有一个RecyclerView(带有LinearLayoutManager)和一个自定义的RecyclerView.ItemDecoration。
让我们说,我想在装饰视图中有按钮(出于某种原因......)。
我用按钮给布局充气,它画得很好。 但我不能使按钮可点击。 如果我按下它,没有任何事情发生(它保持不变,没有按下效果)并且onClick事件没有触发。
ItemDecoration布局的结构是
<LinearLayout>
<TextView/>
<Button/>
</LinearLayout>
而我正试图在ViewHolder中设置侦听器的装饰
class ItemDecorationHolder extends RecyclerView.ViewHolder {
public TextView header;
public Button button;
public HeaderHolder(View itemView) {
super(itemView);
header = (TextView)itemView.findViewById(R.id.header);
button = (Button)itemView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//.. Show toast, etc.
}
});
}
}
我正在onDrawOver方法中绘制装饰。 (实际上,我正在修改此代码库: https : //github.com/edubarr/header-decor )
有任何想法吗? 它可行吗?
谢谢!
当真实标题滚动离开屏幕时,可见标题直接在画布上绘制,而不是像普通的交互式小部件。
你有这些选择
如果选择1/2,Button.setPressed(true)并重绘标题将具有视觉效果。
除了尼尔所说的,这里的答案可能会有所帮助。 将RecycEViewView.OnItemTouchListener中的MotionEvent传递给GestureDetectorCompat
然后你只需要计算标题的高度,看看点击是否落在标题视图上并自己处理事件。
private class RecyclerViewOnGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
float touchY = e.getY();
ALLog.i(this, "Recyclerview single tap confirmed y: " + touchY);
//mGroupHeaderHeight is the height of the header which is used to determine whether the click is inside of the view, hopefully it's a fixed size it would make things easier here
if(touchY < mGroupHeaderHeight) {
int itemAdapterPosition = mRecyclerView.getLayoutManager().getPosition(mRecyclerView.findChildViewUnder(0, mGroupHeaderHeight));
//Do stuff here no you have the position of the item that's been clicked
return true;
}
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onDown(MotionEvent e) {
float touchY = e.getY();
if(touchY < mGroupHeaderHeight) {
return true;
}
return super.onDown(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
float touchY = e.getY();
if(touchY < mGroupHeaderHeight) {
return true;
}
return super.onSingleTapUp(e);
}
}
正如尼尔指出的那样,事情变得越来越复杂。 但是根据定义你不能。
那么,为什么不包括这样做的好图书馆呢? 我建议在我的FlexibleAdapter项目中为可点击的粘贴标头做出努力,该项目使用真实视图(不是装饰器)来处理粘贴时标题上的点击事件。
该部分还有一个工作演示和一个Wiki页面 (不仅如此)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.