可点击 RecyclerView 中的项目
Clickable on items in RecyclerView
我无法单击 RecyclerView 中的项目。例如,如代码所示,我为 Tittle 提供了点击功能,但我现在无法点击。同理我给deleteimage添加了点击功能,但是点击不了。我需要做什么才能使项目可点击?问题出在哪里?我无法点击 RecyclerView 中的项目,这就是问题所在。
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 Class 来源:
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());
}
}
});
}
我遇到了同样的问题。在尝试了很多事情之后,唯一有效的方法是在项目布局的 XML 上添加一个属性:
onClick = 要触发的方法/函数的名称
通过覆盖适配器的 onBindViewHolder 方法在回收器项目上设置点击监听器:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.myTextView.setOnClickListener(...);
}
在这里你可以找到一个例子:https://developer.android.com/guide/topics/ui/layout/recyclerview
我无法单击 RecyclerView 中的项目。例如,如代码所示,我为 Tittle 提供了点击功能,但我现在无法点击。同理我给deleteimage添加了点击功能,但是点击不了。我需要做什么才能使项目可点击?问题出在哪里?我无法点击 RecyclerView 中的项目,这就是问题所在。
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 Class 来源:
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());
}
}
});
}
我遇到了同样的问题。在尝试了很多事情之后,唯一有效的方法是在项目布局的 XML 上添加一个属性: onClick = 要触发的方法/函数的名称
通过覆盖适配器的 onBindViewHolder 方法在回收器项目上设置点击监听器:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.myTextView.setOnClickListener(...);
}
在这里你可以找到一个例子:https://developer.android.com/guide/topics/ui/layout/recyclerview