android - RecyclerView 未被点击

标签 android android-recyclerview adapter onclicklistener

我有一个 RecyclerView 适配器。我还有其他 RecyclerView 适配器,点击它们没有问题。但是,当我单击其中一个 RecyclerView 项目时,不会显示 toast 消息。为什么?

public class AttendanceAdapter extends RecyclerView.Adapter {
    //...
    //other overridden factory methods of RecyclerView Adapter and //Constructors  and data setting Methods 
    //...

    public class AttendanceListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView rollNo, name;
        ImageView isPreset;

        public AttendanceListViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            // ...
            // Layout Items initialization
            // ...
        }

        @Override
        public void onClick(View v) {
            Toast.makeText(mContext,"reyclerViewClicked",Toast.LENGTH_LONG).show();
        }
    }
}

编辑:

我的 itemView XML 看起来像这样..

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="horizontal">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="10"
        android:padding="5dp">

        <TextView
            android:id="@+id/roll_no_in_attendance_single_item"

            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="2"
            android:textColor="@color/colorTeal"
            android:textSize="18sp"
            android:gravity="center"
            android:fontFamily="sans-serif-condensed"
            android:text="1" />


        <TextView
            android:id="@+id/name_in_attendance_single_item"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="6"
            android:textColor="@color/colorTeal"
            android:textSize="18sp"
            android:gravity="center"
            android:fontFamily="sans-serif-condensed"
            android:text="1" />


        <ImageView
            android:layout_margin="10dp"
            android:id="@+id/present_or_absent_toggle_in_attendance_single_item"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_check_circle_black_24dp"
            android:tint="@color/colorTeal"
            android:layout_weight="2"

            android:text="1" />

    </LinearLayout>
</ScrollView>


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

最佳答案

已解决:我在每个项目中使用 ScrollView 时(不知不觉地)犯了一个错误,

千万不要用Scroll View进去

回收 View

项目,因为

回收 View 滚动本身。

关于android - RecyclerView 未被点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36421270/

相关文章:

java - 映射器是适配器模式的一个版本吗

java - 我的应用程序打开后一直崩溃

android - 处理新的存储API

java - 为什么 RecyclerView onBindViewHolder 只调用一次?

java - 如何从ArrayAdapter列表中启动新的Fragment?

java - 使用特定对象上的 onItemClick 获取 listView 中项目的位置

android - 如何处理 Android MediaCodec 解码器的第一个输出字节缓冲区?

android - 如果在 onCreate() 中设置,为什么 ViewTreeObserver > OnScrollChangedListener 在 Android 2.x 上不被调用?

java - 我无法从其他 Intent 将项目添加到我的 RecyclerView

android - 在 onCreateViewHolder 中创建的 ViewHolder 中手动创建的 View