如何在通过itemDecoration添加的RecyclerView粘性标题中附加OnClickListener?

4
我正在尝试在RecyclerView中实现stickyHeaders。每个Header都有至少两个视图(TextView和ImageView),我想单独监听每个视图的点击事件,以便将它们带到不同的活动中。
我尝试了recyclerview-stickyheaders库:http://eowise.github.io/recyclerview-stickyheaders/,在使用这个库时,发现它基于OnItemTouchListener监听整个Header。有没有一种方法可以监听单个视图的点击事件?
除此之外,我还尝试直接在BigramHeaderAdapter(库示例中的Header Adapter)中添加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(头部适配器)内创建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。
@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);
        }
    });
}

以上的代码(1和2)是否与库一起工作? - Bharatesh
@bharat 两次尝试都无法使用该库。 - dinesh
1个回答

1
很晚了,但我仍然设法通过更改StickyRecyclerHeadersTouchListener中存在的SingleTapDetector类来实现触摸监听器。

这是我正在使用的粘性标题布局。

<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类型。如果它是相对布局,则我会获取其内部的子元素。一旦我获取了子元素,就使用触摸位置和元素的默认位置来确定是否在靠近该元素的任何地方被触摸。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接