当前位置: 首页 > 知识库问答 >
问题:

如何将OnClickListener附加到通过Item装饰添加的RecyerView中的stickyHeader

燕扬
2023-03-14

尝试Recycerview-StickyHeaders库:http://eowise.github.io/Recycerview-StickyHeaders/

在使用这个库时,发现它监听基于OnItemTouchListener的整个header。有没有一种方法可以收听个人视图上的点击?

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

标题布局(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>
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);
    }
@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个答案

嵇浩然
2023-03-14

虽然为时已晚,但我还是通过更改StickyRecyerHeaderStouchListener的中的SingletapDetector类实现了touch侦听器。

这是我使用的粘稠标题的布局

<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;
}}
 类似资料:
  • 问题内容: 我正在尝试使用最新的REST API发布JIRA附件。这是我的代码: 但是,我得到以下回应: 我的本地JIRA实例中确实存在密钥TEST-2的问题,我可以在Jira应用程序本身中“手动”添加附件。我知道我必须添加类型为“ X-Atlassian- Token:nocheck”的标头以防止XSRF,但是从输出来看,我必须做错了什么。令我更加困惑的是,在XSRF检查失败。 我已经在Goog

  • 我有一个lambda函数,它读取Dynamodb流。我已将Dynamodb流ARN从同一AWS帐户中的另一个堆栈导出。现在,在Lambda中添加eventSource时,它从表构造中请求。 裁判:https://awslabs.github.io/aws-cdk/refs/_aws-cdk_aws-lambda-event-sources.html#dynamodb-溪流 但是我有一条小溪。是否有

  • 我正在使用TCPDF从php文件创建PDF文件。一切正常。现在,我想使用现有页面添加一个附加页面。来自服务器的pdf文件。 最好的方法是使用FPDI afaik。 但我找不到任何关于如何在TCPDF中设置FPDI以添加页面的文档或工作示例。我所看到的只是如何使用外部pdf作为标题或背景等。 像这个https://www.setasign.com/products/fpdi/about/ 我在TCP

  • 问题内容: 我正在尝试将文件附加到通知邮件中的管道。 我怎样才能做到这一点?一个例子会很好。 问题答案: email-ext使用Ant文件语法 例如用法:

  • 我正在处理谷歌表格结果数据,这些数据被放入谷歌表格选项卡,我添加了一列来计算测验分数的百分比,然后在计算完成百分比的同一张表格中被拉到跟踪选项卡上。现在,无论我尝试了哪个公式,百分比列都显示为空白字段。我想知道是否有一个不同的公式将工作,将自动适用于列时,新的响应被添加?或者谷歌脚本是更好的选择吗? 我想保持原始结果数据在同一张表上,因为它正在将所有的测验编译成一张谷歌表,其中一个标签拉动百分比以

  • 问题内容: 假设我有一些要将JavaScript操作添加到的链接: 当页面加载时,我给他们所有的click事件: 但让我们说之后,我添加了另一个元素,但我想给它相同的事件。我不能这样做: 因为前三个事件将包含两个事件。处理此问题的最佳方法是什么? 问题答案: 您可以将$ .on绑定到这样的dom中始终存在的父元素。 请注意: 您可以用dom中将始终存在的元素的任何父级替换,并且父级越近越好。 具有