回收站视图中的行项目不响应点击事件

Row item in recycler view does not respond to click event

以下是我的行项目的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">


    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img_query"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_gravity="left|top"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="2dp"
            android:src="@drawable/ic_anoun_red" />

        <TextView
            android:id="@+id/txt_ques_sub"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:layout_marginTop="12dp"
            android:layout_toRightOf="@+id/img_query"
            android:text="Problem in Merge sort"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/primary_text_light"
            android:textStyle="bold" />



        <TextView

            android:id="@+id/txt_ques_detail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="2dp"
            android:layout_below="@+id/img_query"
            android:text="@string/sample_query_detail"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/primary_text_light" />

        <TextView
            android:id="@+id/txt_asked_by"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txt_ques_detail"
            android:layout_marginRight="4dp"
            android:layout_marginTop="2dp"
            android:text="Tabish Butt"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/lightorange" />

        <TextView
            android:id="@+id/txt_ques_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txt_asked_by"
            android:layout_marginRight="4dp"
            android:text="07:15 am"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/secondary_text_dark" />

        <TextView
            android:id="@+id/txt_ques_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txt_ques_time"
            android:layout_marginRight="4dp"
            android:text="25 SEP 2015"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/secondary_text_dark" />

        <TextView
            android:id="@+id/txt_replies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/txt_ques_date"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="4dp"
            android:text="@string/default_num_answers"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/primary_dark_material_light" />


        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_below="@+id/txt_replies"
            android:alpha="0.12"
            android:background="@android:color/black" />
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/divider"
            android:orientation="vertical">

            <Button
                android:id="@+id/btn_answer"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:drawableLeft="@android:drawable/ic_media_play"
                android:text="ANSWER"
                android:textColor="@color/primary_dark_material_light" />
        </LinearLayout>
    </RelativeLayout>
    </LinearLayout>

</LinearLayout>

点击代码如下:

holder.setClickListener(new MyViewHolder.ClickListener() {
            @Override
            public void onClick(View v, int position, boolean isLongClick) {
                if(isLongClick)
                {
                    /* TASK TO PERFORM AT LONG CLICKS */
                    Toast.makeText(context, "Long click acheived", Toast.LENGTH_LONG).show();
                }
                else
                {
                    /* TASK TO PERFORM AT CLICKS */
                    Toast.makeText(context, "Click Event", Toast.LENGTH_LONG).show();
                }
            }
        });

现在,当我 运行 这一切正常时,除了单击行项目不会产生任何响应(在这种情况下为祝酒词)

任何帮助将不胜感激。需要帮助!

下面是MyViewHolder中处理点击事件的方法class:

static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {

    //Following represent grid_layout item
    TextView txtFileName;
    TextView txtFileIssueTime;
    Button btnAnswer;
    private ClickListener clickListener;
    public MyViewHolder(View itemView) {
        super(itemView);

        //VIEWS IN LAYOUT BEING INITIALIZED

        btnAnswer = (Button) itemView.findViewById(R.id.btn_answer);
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }

    /* Interface for handling clicks - both normal and long ones. */
    public interface ClickListener {

        /**
         * Called when the view is clicked.
         *
         * @param v view that is clicked
         * @param position of the clicked item
         * @param isLongClick true if long click, false otherwise
         */
        public void onClick(View v, int position, boolean isLongClick);

    }

    /* Setter for listener. */
    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public void onClick(View v) {

        // If not long clicked, pass last variable as false.
        clickListener.onClick(v, getAdapterPosition(), false);
    }

    @Override
    public boolean onLongClick(View v) {

        // If long clicked, passed last variable as true.
        clickListener.onClick(v, getAdapterPosition(), true);
        return true;
    }

}

无需为 itemView 设置点击监听器,您已经在 ViewHolder 中实现了点击监听器。

我仍然不确定你是从哪里调用 "holder.setClickListener(...)",但假设你是从 Activity 调用的,我宁愿遵循这种方法:

1) 将 ClickListener 作为一个独立的接口(在你的 holder 之外),你以后可以在不同的适配器中使用它。

2) 将 ClickListener 添加到您的适配器和 ViewHolder。

3) 设置适配器时,将您的 Activity/Fragment 作为侦听器传递(它们应该实现 ClickListener 接口)

4) 创建ViewHolder时,将监听器也传递给ViewHolder,并将其设置为变量(您已定义)。

5) 正如您已经做的那样,在 onClick 方法中,调用 clickListener 方法。

如果可行,请告诉我。这正是我经常做的。

尝试将 clickable 移动到父 LinearLayout 中,而不是嵌套的 LinearLayout

编辑:

实际上,您的布局应该只是

<FrameLayout
    android:background:"@color/white"
    android:foreground:"?selectableItemBackground">

    <RelativeLayout />

</FrameLayout>