如何将 OnClickListener 附加到通过 itemDecoration 添加的 RecyclerView 中的 stickyHeader
How to attach OnClickListener to stickyHeader in RecyclerView which is added via itemDecoration
我正在尝试在 RecyclerView 中实现粘性Headers。每个 Header 将至少有两个视图(TextView 和 ImageView),我想分别收听每个视图的点击,以便我可以将它们带到不同的活动中。
尝试了 recyclerview-stickyheaders 库:http://eowise.github.io/recyclerview-stickyheaders/
在使用这个库时,发现它会监听整个 header 基于 OnItemTouchListener。有没有办法听取对个别视图的点击?
除此之外,我尝试直接在 BigramHeader 适配器(Header 库示例中的适配器)中添加 OnClickListeners,如下所述。
我想知道是否有办法实现这一点。请帮忙!
Header布局(top_header.xml示例):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true"
android:paddingStart="@dimen/item_padding"
android:paddingEnd="@dimen/item_padding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/title"
android:layout_width="40dp"
android:layout_height="30dp"
android:clickable="true"
android:gravity="left|center_vertical"
android:textAppearance="?android:textAppearanceMedium"
android:background="?android:colorBackground"/>
<View
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ImageView
android:id="@+id/image"
android:clickable="true"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="?android:listDivider"
android:scaleType="fitXY"/>
</LinearLayout>
尝试 1:在 BigramHeaderAdapter(Header 适配器)
中创建 ViewHolder 时实现 View.OnClickListener
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@Override
public void onClick(View v) {
mHeaderListener.onThread(v, getPosition());
}
public TextView title;
public ImageView image;
public ViewHolder(View itemView, HeaderViewHolderClicks listener) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
image = (ImageView) itemView.findViewById(R.id.image);
mHeaderListener = listener;
title.setOnClickListener(this);
itemView.setOnClickListener(this);
}
public interface HeaderViewHolderClicks {
public void onThread(View view, int position);
}
尝试 2:在 BigramHeaderAdapter 中的 onBindViewHolder 中添加了 OnClickListener,这是 header 适配器。
@Override
public void onBindViewHolder(ViewHolder headerViewHolder, final int position) {
headerViewHolder.title.setText(items.get(position).subSequence(0, 2));
Log.v("binded", items.get(position).subSequence(0, 2).toString());
headerViewHolder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("BigramHeaderAdapter : ", "Click on Thread " + position);
}
});
headerViewHolder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Image click", " Clicked on Image " + position);
}
});
headerViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Image click", " Clicked on ItemView " + position);
}
});
}
已经很晚了,但我仍然设法通过更改 StickyRecyclerHeadersTouchListener's
中的 SingleTapDetector
class 来实现触摸监听器。
这是我正在使用的粘性页眉布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="2dp">
<TextView
android:id="@+id/stickyheader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px" />
<TextView
android:id="@+id/stickyseemore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="@string/seemore"
android:textColor="@color/textPrimary"
android:textSize="20px" />
这是检测触摸的代码
private class SingleTapDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e) {
int position = mDecor.findHeaderPositionUnder((int) e.getX(), (int) e.getY());
boolean touch=false;
if (position != -1) {
View headerView = mDecor.getHeaderView(mRecyclerView, position);
long headerId = getAdapter().getHeaderId(position);
if (headerView instanceof RelativeLayout) {
View nextChild = ((RelativeLayout)headerView).getChildAt(1);
if(nextChild.getX()<e.getX()){
touch=true;
}
}
mOnHeaderClickListener.onHeaderClick(headerView, position, headerId,touch);
mRecyclerView.playSoundEffect(SoundEffectConstants.CLICK);
headerView.onTouchEvent(e);
return true;
}
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}}
我在这里检查 headerView 类型。如果它的相对布局,那么我将在其中获取子元素。一旦我得到子元素,然后使用触摸的位置和我的元素的默认位置,我确定它是否在靠近该元素的任何地方被触摸。
我正在尝试在 RecyclerView 中实现粘性Headers。每个 Header 将至少有两个视图(TextView 和 ImageView),我想分别收听每个视图的点击,以便我可以将它们带到不同的活动中。
尝试了 recyclerview-stickyheaders 库:http://eowise.github.io/recyclerview-stickyheaders/
在使用这个库时,发现它会监听整个 header 基于 OnItemTouchListener。有没有办法听取对个别视图的点击?
除此之外,我尝试直接在 BigramHeader 适配器(Header 库示例中的适配器)中添加 OnClickListeners,如下所述。
我想知道是否有办法实现这一点。请帮忙!
Header布局(top_header.xml示例):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true"
android:paddingStart="@dimen/item_padding"
android:paddingEnd="@dimen/item_padding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/title"
android:layout_width="40dp"
android:layout_height="30dp"
android:clickable="true"
android:gravity="left|center_vertical"
android:textAppearance="?android:textAppearanceMedium"
android:background="?android:colorBackground"/>
<View
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ImageView
android:id="@+id/image"
android:clickable="true"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="?android:listDivider"
android:scaleType="fitXY"/>
</LinearLayout>
尝试 1:在 BigramHeaderAdapter(Header 适配器)
中创建 ViewHolder 时实现 View.OnClickListenerpublic static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@Override
public void onClick(View v) {
mHeaderListener.onThread(v, getPosition());
}
public TextView title;
public ImageView image;
public ViewHolder(View itemView, HeaderViewHolderClicks listener) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
image = (ImageView) itemView.findViewById(R.id.image);
mHeaderListener = listener;
title.setOnClickListener(this);
itemView.setOnClickListener(this);
}
public interface HeaderViewHolderClicks {
public void onThread(View view, int position);
}
尝试 2:在 BigramHeaderAdapter 中的 onBindViewHolder 中添加了 OnClickListener,这是 header 适配器。
@Override
public void onBindViewHolder(ViewHolder headerViewHolder, final int position) {
headerViewHolder.title.setText(items.get(position).subSequence(0, 2));
Log.v("binded", items.get(position).subSequence(0, 2).toString());
headerViewHolder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("BigramHeaderAdapter : ", "Click on Thread " + position);
}
});
headerViewHolder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Image click", " Clicked on Image " + position);
}
});
headerViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Image click", " Clicked on ItemView " + position);
}
});
}
已经很晚了,但我仍然设法通过更改 StickyRecyclerHeadersTouchListener's
中的 SingleTapDetector
class 来实现触摸监听器。
这是我正在使用的粘性页眉布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="2dp">
<TextView
android:id="@+id/stickyheader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px" />
<TextView
android:id="@+id/stickyseemore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="@string/seemore"
android:textColor="@color/textPrimary"
android:textSize="20px" />
这是检测触摸的代码
private class SingleTapDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e) {
int position = mDecor.findHeaderPositionUnder((int) e.getX(), (int) e.getY());
boolean touch=false;
if (position != -1) {
View headerView = mDecor.getHeaderView(mRecyclerView, position);
long headerId = getAdapter().getHeaderId(position);
if (headerView instanceof RelativeLayout) {
View nextChild = ((RelativeLayout)headerView).getChildAt(1);
if(nextChild.getX()<e.getX()){
touch=true;
}
}
mOnHeaderClickListener.onHeaderClick(headerView, position, headerId,touch);
mRecyclerView.playSoundEffect(SoundEffectConstants.CLICK);
headerView.onTouchEvent(e);
return true;
}
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}}
我在这里检查 headerView 类型。如果它的相对布局,那么我将在其中获取子元素。一旦我得到子元素,然后使用触摸的位置和我的元素的默认位置,我确定它是否在靠近该元素的任何地方被触摸。