【问题标题】:Recycle View holder on click listener not triggered未触发 Recyclerview 持有者 onclick 侦听器
【发布时间】:2019-06-19 22:25:19
【问题描述】:

我有一个监听器的 recyclerview。我使用此代码:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {


   //just setting the textview etc



    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Model_NewProg model_newProg = ListOfExo.get(position);
            openDialog(model_newProg, position);
        }
    });
}

我的适配器 xml 是:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="5dp"
    card_view:cardBackgroundColor="#FFFFFF"
    card_view:cardCornerRadius="5dp">



    <RelativeLayout
        android:layout_width="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:layout_height="wrap_content">

        <View
            android:id="@+id/view_space"
            android:layout_width="match_parent"
            android:background="@color/common_google_signin_btn_text_light_pressed"
            android:layout_height="5dp">


        </View>
    <LinearLayout
        android:id="@+id/lay1"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_below="@id/view_space"
        android:layout_height="wrap_content">



        <TextView
            android:id="@+id/lbl_exo_name"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:gravity="center"
            android:textColorHint="#aeafaf"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/lbl_temps_pause"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:inputType="number"
            android:gravity="center"
            android:textColorHint="#aeafaf"
            android:textSize="15dp" />


    </LinearLayout>



    <LinearLayout
        android:id="@+id/lay2"
        android:layout_width="match_parent"
        android:layout_below="@id/lay1"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/lbl_nbr_séries"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:inputType="number"
            android:gravity="center"
            android:textColorHint="#aeafaf"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/lbl_nbr_rep"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:inputType="number"
            android:gravity="center"
            android:textColorHint="#aeafaf"
            android:textSize="15dp" />

    </LinearLayout>


    </RelativeLayout>


</android.support.v7.widget.CardView>

由于某种原因,如果我单击单元格,则支架上的侦听器不会触发。如果我瞄准文本视图的角落,有时它会触发。但似乎文本视图正在“阻止”侦听器......我必须尝试添加 android:clickable=false;安卓:可点击=真; ,... 不工作

有人有想法吗?我每次都使用这段代码,它总是有效的

【问题讨论】:

  • 在 ViewHolder 类中使用 onclick 监听器,

标签: android android-recyclerview listener


【解决方案1】:

holder.itemView 必须更改为:holder.theIdOfTheElementYouWantToClick

【讨论】:

    【解决方案2】:

    id 分配给您的cardView 并在ViewHolder 类中获取它,然后在ViewHolder 类中对这个id 执行onClick。例如.. 如果您的 cardView idcardView

    class ViewHolder extends RecyclerView.ViewHolder {
    
        public CardView rowCardView;
    
        public ViewHolder(View itemView) {
            super(itemView);
            rowCardView = (CardView) itemView.findViewById(R.id.cardView);
            rowCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //add your code..
                }
            });
    
        }}
    

    【讨论】:

    • 它不工作这很奇怪,只有当我点击单元格的左角时才有效
    • 我很确定这是父母的问题或类似的问题我什至尝试在 onBindViewHolder 方法上放置一个 CardView 的侦听器,但它仍然没有触发,就像 textview 一样点击而不是 CardView
    • 它正在发生
    • @ImNeos 尝试给出 RelativeLayout width = match_parent
    【解决方案3】:

    您需要在ViewHolder 类中实现View.onClickListener,然后在constructure 中注册它,请参阅this 答案以获取更多详细信息

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    
    
        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
    }
     @Override
            public void onClick(View v) {
    //do what you want
    }
    
        }
    

    【讨论】:

    • 它不起作用。如果我将侦听器放在所有文本视图中,它会起作用,但这不是我想要的。我希望当我单击单元格上的任意位置时它会触发侦听器。通常 holder.ItemView.setOnClickListener 应该可以工作,但我不明白这里有什么问题
    • 所以你必须自己注册 itemview 来做你想做的事,这是正确的方法,因为在 onBindViewHolder 中放置一个 lisener 是个坏主意
    • 是的,我明白了,我正在尝试按照您的方式进行操作,但是在 onCreateViewHolder 中的行 ViewHolder viewHolder = new ViewHolder(v);不工作。它迫使我写:“RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(v);”它说 ViewHolder 是抽象的,它不能被实例化也许我错过了一个库?
    • @MohammadSommakia 你复制了我的答案..它不是 ImageView 它的 CardView.. ;)
    猜你喜欢
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多