首页 文章

如何将OnClickListener附加到RecyclerView中的stickyHeader,它通过itemDecoration添加

提问于
浏览
3

我正在尝试在RecyclerView中实现stickyHeaders . 每个 Headers 将至少有两个视图(TextView和ImageView),我想分别听取每个视图的点击,以便我可以将它们带到不同的活动 .

试过的recyclerview-stickyheaders图书馆:http://eowise.github.io/recyclerview-stickyheaders/

在玩这个库的过程中,想到这会侦听基于OnItemTouchListener的整个头文件 . 有没有办法听取个别观点的点击?

除此之外,我尝试直接在BigramHeaderAdapter(库的示例中的头部适配器)中添加OnClickListener,如下所述 .

我想知道是否有办法实现这一目标 . 请帮忙!

Headers 布局(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 Adapter)中创建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 回答

  • 1

    它很晚,但我仍然设法通过更改 StickyRecyclerHeadersTouchListener's 中的 SingleTapDetector 类来实现触摸侦听器 .

    这是我正在使用的粘性 Headers 的布局

    <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类型 . 如果它的相对布局,那么我在其中获取子元素 . 一旦我获得了子元素,然后使用触摸位置和元素的默认位置,我确定它是否被触及到更靠近该元素的位置 .

相关问题