Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.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
Android 可单击RecyclerView中的项目_Android_Android Recyclerview_Android Adapter - Fatal编程技术网

Android 可单击RecyclerView中的项目

Android 可单击RecyclerView中的项目,android,android-recyclerview,android-adapter,Android,Android Recyclerview,Android Adapter,我无法在RecyclerView中单击项目。例如,如代码所示,我为Title提供了单击功能,但我现在无法单击。以同样的方式,我给deleteimage一个单击功能,但我不能单击。我需要做什么才能使项目可点击?问题在哪里?我无法点击RecyclerView中的项目,这就是问题所在 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"

我无法在RecyclerView中单击项目。例如,如代码所示,我为Title提供了单击功能,但我现在无法单击。以同样的方式,我给deleteimage一个单击功能,但我不能单击。我需要做什么才能使项目可点击?问题在哪里?我无法点击RecyclerView中的项目,这就是问题所在

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/productImage"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/roomlocation" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="9dp"
                android:text="productAdi"
                android:textSize="19dp"
                android:clickable="true"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/productDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="3dp"
                android:text="Cihaza ait eklenmiş odalar"
                android:textSize="14dp"
                android:textStyle="italic" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/deleteproduct"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="5dp"
                android:clickable="true"
                android:src="@drawable/ic_delete_yellow" />

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

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="stechome.berkeylen.firebasedatabase.RoomsActivity">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvRooms"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>
XML源代码:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/productImage"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/roomlocation" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="9dp"
                android:text="productAdi"
                android:textSize="19dp"
                android:clickable="true"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/productDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="3dp"
                android:text="Cihaza ait eklenmiş odalar"
                android:textSize="14dp"
                android:textStyle="italic" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/deleteproduct"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="5dp"
                android:clickable="true"
                android:src="@drawable/ic_delete_yellow" />

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

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="stechome.berkeylen.firebasedatabase.RoomsActivity">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvRooms"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>

RecyclerView类来源:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/productImage"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/roomlocation" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="9dp"
                android:text="productAdi"
                android:textSize="19dp"
                android:clickable="true"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/productDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="3dp"
                android:text="Cihaza ait eklenmiş odalar"
                android:textSize="14dp"
                android:textStyle="italic" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/deleteproduct"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="5dp"
                android:clickable="true"
                android:src="@drawable/ic_delete_yellow" />

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

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="stechome.berkeylen.firebasedatabase.RoomsActivity">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvRooms"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>
    public class RoomsAdapter extends RecyclerView.Adapter<RoomsAdapter.MyViewHolder> {
        DatabaseReference ref = mDatabase.child("0").child("Rooms");

          public class MyViewHolder extends RecyclerView.ViewHolder {
             public MyViewHolder(View view) {
                super(view);
                productImage = (ImageView)          

                itemView.findViewById(R.id.productImage);
                title = (TextView) view.findViewById(R.id.title);
                deleteImage = (ImageView) view.findViewById(R.id.deleteproduct);

ref.addValueEventListener(new ValueEventListener() {
                    @Override
           public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                            String data = snapshot.getValue(String.class);
                            Rooms.add(data);

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

                                    int position = 0;
                                    position = getAdapterPosition();
                                    if (getAdapterPosition() == position){
                                        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("0");
                                        rootRef.child("titles").child("0").child("places").setValue(Rooms.get(position));
                                    }

                                }
                            });
                        }

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

                     if (v == deleteproduct) {
                                  deleteproduct(getLayoutPosition());
                                }
                            }
                        });
                    }
公共类RoomsAdapter扩展了RecyclerView.Adapter{
DatabaseReference=mDatabase.child(“0”).child(“房间”);
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
公共MyViewHolder(视图){
超级(视图);
productImage=(ImageView)
itemView.findviewbyd(R.id.productImage);
title=(TextView)view.findViewById(R.id.title);
deleteImage=(ImageView)view.findViewById(R.id.deleteproduct);
ref.addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
对于(DataSnapshot快照:DataSnapshot.getChildren()){
String data=snapshot.getValue(String.class);
添加(数据);
title.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
int位置=0;
位置=getAdapterPosition();
if(getAdapterPosition()==位置){
DatabaseReference rootRef=FirebaseDatabase.getInstance().getReference().child(“0”);
rootRef.child(“标题”).child(“0”).child(“位置”).setValue(Rooms.get(position));
}
}
});
}
deleteImage.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(v==deleteproduct){
deleteproduct(getLayoutPosition());
}
}
});
}

我也遇到了同样的问题。在尝试了许多方法后,唯一有效的方法是在项目布局的XML上添加一个atribute:
onClick=要触发的方法/函数的名称

通过覆盖适配器的onBindViewHolder方法,设置回收器项上的单击侦听器:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/productImage"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/roomlocation" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="9dp"
                android:text="productAdi"
                android:textSize="19dp"
                android:clickable="true"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/productDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="3dp"
                android:text="Cihaza ait eklenmiş odalar"
                android:textSize="14dp"
                android:textStyle="italic" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/deleteproduct"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="5dp"
                android:clickable="true"
                android:src="@drawable/ic_delete_yellow" />

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

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="stechome.berkeylen.firebasedatabase.RoomsActivity">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvRooms"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.myTextView.setOnClickListener(...);
}

在这里,您可以找到一个示例:

我无法单击RecyclerView中的项目,这就是问题所在。是的,我知道您无法单击RecyclerView中的项目,但我在适配器代码中看到的一切在我看来都是错的。1.您获取活动中的数据,然后将其传递给适配器。2.您在onBind中为click listener编写代码适配器的ViewHolder,而不是ViewHolder。您只需将onClickListener一次设置为一个元素。它太宽泛,无法解释RecyclerView的实际工作原理。我只能说您需要更多有关RecyclerView的课程
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/productImage"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/roomlocation" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="9dp"
                android:text="productAdi"
                android:textSize="19dp"
                android:clickable="true"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/productDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="3dp"
                android:text="Cihaza ait eklenmiş odalar"
                android:textSize="14dp"
                android:textStyle="italic" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/deleteproduct"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="5dp"
                android:clickable="true"
                android:src="@drawable/ic_delete_yellow" />

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

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="stechome.berkeylen.firebasedatabase.RoomsActivity">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvRooms"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>