【问题标题】:RecyclerView ItemClickListener is not working in OnBindViewHolderRecyclerView ItemClickListener 在 OnBindViewHolder 中不起作用
【发布时间】:2021-05-23 04:32:31
【问题描述】:

在 RecyclerView 中单击时如何获取特定文本视图的 getText(string) ? 我在 OnBindViewHolder 中尝试过像 holder.itemview.setOnclickListener 一样,但我在日志中没有看到任何内容。

我得到了解决方案,但它只适用于双击。单击任何解决方案???

请检查下面我的适配器类代码

public class AddressAdapter extends FirestoreRecyclerAdapter<AddressModel, AddressAdapter.viewHolder> {
    

    public AddressAdapter(@NonNull FirestoreRecyclerOptions<AddressModel> options) {
        super(options);
    }


    @Override
    protected void onBindViewHolder(@NonNull viewHolder holder, final int position, @NonNull final AddressModel model) {
        holder.name.setText(model.getName());
        holder.locationID.setText(String.valueOf(holder.getAdapterPosition()+1) );
        holder.address.setText(model.getAddress());
        holder.address.setMovementMethod(new ScrollingMovementMethod());
        holder.name.setPaintFlags(holder.name.getPaintFlags() |  Paint.UNDERLINE_TEXT_FLAG);
   
         //Guys this is not works in single click only works in double click
        holder.imgDeleteLoc.setOnClickListener(v -> {
Toast.makeText(v.getContext(), "clicked", Toast.LENGTH_SHORT).show();
         
            Log.d("cl",holder.getText().toString());
        });
    }

    @NonNull
    @Override
    public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.saved_locations, parent, false);
        return new viewHolder(view);
    }


    class viewHolder extends RecyclerView.ViewHolder {
         TextView locationID,name,address;
        ImageView imgDeleteLoc;

        public viewHolder(@NonNull View itemView) {
            super(itemView);
            locationID=itemView.findViewById(R.id.tvAddressIdCounter);
            name=itemView.findViewById(R.id.tvName);
            address=itemView.findViewById(R.id.tvAddress);
            imgDeleteLoc=itemView.findViewById(R.id.imgDeleteLoc);
           
        }
    }
}

下面是我的 Inflator XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cv"
    style="@style/CustomCardViewStyleTop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    app:cardCornerRadius="10dp">

    <!-- Insert UI elements -->

    <TextView
        android:id="@+id/tvAddress"
        android:layout_width="175dp"
        android:layout_height="70dp"
        android:layout_gravity="center|top"
        android:layout_marginTop="90dp"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:scrollbars="vertical"
        android:text="#2/22 Nainiyappan Garden 3rd Lane Old Washermenpet Chennai -600021"
        android:textColor="#77959292" />

    <TextView
        android:id="@+id/tvAddressIdCounter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="25dp"
        android:text="1"
        android:textColor="#000000"
        android:textSize="18sp" />

    <View
        android:layout_width="1dp"
        android:layout_height="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="5dp"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|top"
        android:layout_marginTop="70dp"
        android:text="Shaik"
        android:textColor="#77959292"
        android:textStyle="bold|italic" />

    <View
        android:id="@+id/view3"
        android:layout_width="200dp"
        android:layout_height="1dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="65dp"
        android:layout_marginRight="10dp"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="113dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="15dp"
        android:text="Saved Locations"
        android:textColor="@color/black"
        android:textColorHighlight="#FFFFFF"
        android:textSize="18sp" />

    <ImageView
        android:id="@+id/imgDeleteLoc"
        android:clickable="true"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_baseline_delete_24"
        android:layout_gravity="end"
        android:layout_marginEnd="15dp"
        android:layout_marginTop="15dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </LinearLayout>


</androidx.cardview.widget.CardView>

请任何人在点击 recyclerview 时帮助获取特定的 textview 内容。

【问题讨论】:

  • 分享你的布局文件saved_locations
  • 是的,请检查

标签: java android android-recyclerview textview onitemclicklistener


【解决方案1】:

你只需要打电话

holder.imgDeleteLoc.setOnClickListener(v -> {
        Log.d("cl",holder.address.getText().toString());
    });

【讨论】:

  • 嗯我试过了,但即使我没有看到任何吐司或日志,它仍然无法正常工作
  • 嘿,它可以工作,但不能在单击时工作,它可以通过双击工作,任何解决方案?
【解决方案2】:

您不应该再次使用 findViewById,您可以尝试通过以下任一方式获取文本:

  • holder.address.getText()
  • model.getAddress()

【讨论】:

  • 好的,我试过了,它可以工作,但双击不是单击
【解决方案3】:

首先在这里更新我的代码:

class viewHolder extends RecyclerView.ViewHolder {
         TextView locationID,name,address;
        ImageView imgDeleteLoc;

        public viewHolder(@NonNull View itemView) {
            super(itemView);
            locationID=itemView.findViewById(R.id.tvAddressIdCounter);
            name=itemView.findViewById(R.id.tvName);
            address=itemView.findViewById(R.id.tvAddress);
            imgDeleteLoc=itemView.findViewById(R.id.imgDeleteLoc);
        }
    }

试试这个:

String getAddress = holder.address.getText().toString();

【讨论】:

  • 是的,但不能通过单击来工作
【解决方案4】:

如下更新ImageView或者你可以使用ImageButton

<ImageView
        android:id="@+id/imgDeleteLoc"
        android:clickable="true"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:clickable="true"
        android:focusable="true"
        android:src="@drawable/ic_baseline_delete_24"
        android:layout_gravity="end"
        android:layout_marginEnd="15dp"
        android:layout_marginTop="15dp"/>

【讨论】:

  • @BeginnerDev 尝试大一点的尺寸,比如说 50dp 的高度和宽度
猜你喜欢
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
  • 2018-12-04
  • 2020-12-31
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
相关资源
最近更新 更多