Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击recylerview(cardview)时的超高设置_Java_Android_Android Recyclerview_Onclicklistener_Android Cardview - Fatal编程技术网

Java 单击recylerview(cardview)时的超高设置

Java 单击recylerview(cardview)时的超高设置,java,android,android-recyclerview,onclicklistener,android-cardview,Java,Android,Android Recyclerview,Onclicklistener,Android Cardview,MainActivity.java activity_main.xml cardview xml <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cardview="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"

MainActivity.java

activity_main.xml

cardview xml

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:id="@+id/bookcardview"
android:layout_width="120dp"
android:layout_height="190dp"
android:layout_margin="5dp"
cardview:cardCornerRadius="4dp">

<LinearLayout
    android:id="@+id/bkdetlinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:ignore="ExtraText"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    android:layout_marginEnd="4dp"
    >


    <ImageView


        android:id="@+id/bkdetimg"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:background="#2d2d2d"
        android:clickable="true"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/bkdettitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Book title"
        android:textColor="#2d2d2d"
        android:textSize="12sp" />


</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
我尝试在cardviewholder上单击设置,这是一个单独的xml文件,在我的mainactivity oncreate中膨胀,并在下面设置onclick侦听器 onBindViewHolder但是我得到了一个关于空对象引用的错误,好像活动无法检测到cardview

您可以在onBindViewHolder内的RecyclerView项上设置OnClickListener,如下所示:

@Override
protected void onBindViewHolder(@NonNull Bkhomeholder holder, int position, @NonNull Bookdeets model) {

    ....

    holder.title.setText(model.getBookname());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Do your operation here
        }
    });

}
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:foreground="?android:attr/selectableItemBackground"
    android:id="@+id/bookcardview"
    android:layout_width="120dp"
    android:layout_height="190dp"
    android:layout_margin="5dp"
    cardview:cardCornerRadius="4dp">

    <LinearLayout
        android:id="@+id/bkdetlinear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="ExtraText"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_marginEnd="4dp"
        >


        <ImageView


            android:id="@+id/bkdetimg"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:background="#2d2d2d"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/bkdettitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Book title"
            android:textColor="#2d2d2d"
            android:textSize="12sp" />


    </LinearLayout>
</androidx.cardview.widget.CardView>
更新:从cardview布局中删除android:clickable=true,如下所示:

@Override
protected void onBindViewHolder(@NonNull Bkhomeholder holder, int position, @NonNull Bookdeets model) {

    ....

    holder.title.setText(model.getBookname());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Do your operation here
        }
    });

}
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:foreground="?android:attr/selectableItemBackground"
    android:id="@+id/bookcardview"
    android:layout_width="120dp"
    android:layout_height="190dp"
    android:layout_margin="5dp"
    cardview:cardCornerRadius="4dp">

    <LinearLayout
        android:id="@+id/bkdetlinear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="ExtraText"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_marginEnd="4dp"
        >


        <ImageView


            android:id="@+id/bkdetimg"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:background="#2d2d2d"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/bkdettitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Book title"
            android:textColor="#2d2d2d"
            android:textSize="12sp" />


    </LinearLayout>
</androidx.cardview.widget.CardView>

在适配器中找到cardview id,然后单击列表将其设置为adapter@AmanBaid,从卡视图中删除所有android:clickable=true并选中
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:foreground="?android:attr/selectableItemBackground"
    android:id="@+id/bookcardview"
    android:layout_width="120dp"
    android:layout_height="190dp"
    android:layout_margin="5dp"
    cardview:cardCornerRadius="4dp">

    <LinearLayout
        android:id="@+id/bkdetlinear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="ExtraText"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_marginEnd="4dp"
        >


        <ImageView


            android:id="@+id/bkdetimg"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:background="#2d2d2d"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/bkdettitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Book title"
            android:textColor="#2d2d2d"
            android:textSize="12sp" />


    </LinearLayout>
</androidx.cardview.widget.CardView>