如何为recyclerview中动态添加的项目设置OnclickListener?

How to set OnclickListener for the dynamically added items in recyclerview?

在我的应用程序中,我使用回收器视图来显示一些数据,并且为回收器视图中的每个项目添加一个已检查文本视图列表。这些选中的文本视图是根据提供的列表的大小添加的,并且工作正常。现在我想为这些项目添加点击侦听器以了解它们是否被选中?

检查项目后,应将这些检查项目带到另一个 activity。但现在点击侦听器直接添加到回收站视图中,这将无法访问我来执行开箱即用代码。所以我正在寻找如何添加点击侦听器并在另一个 activity.

中访问它

我的 Recyclerview 代码:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.allio.customer.R;
import com.allio.customer.models.Types_Item;
import com.bumptech.glide.Glide;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.github.florent37.expansionpanel.ExpansionLayout;
import com.github.florent37.expansionpanel.viewgroup.ExpansionLayoutCollection;

public class Types_adapter extends FirestoreRecyclerAdapter<Types_Item, Types_adapter.RecyclerHolder> {

    private final ExpansionLayoutCollection expansionsCollection = new ExpansionLayoutCollection();
    Context context;

    public Types_adapter(@NonNull FirestoreRecyclerOptions<Types_Item> options, Context context) {
        super(options);
        expansionsCollection.openOnlyOne(true);
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull RecyclerHolder holder, int position, @NonNull Types_Item model) {
        expansionsCollection.add(holder.getExpansionLayout());
        holder.textView.setText(model.getName());
        Glide.with(holder.imageView.getContext())
                .load(model.getImageURL())
                .into(holder.imageView);
        CheckedTextView[] textView = new CheckedTextView[model.getProblems().size()];
        holder.problems.removeAllViews();
        for (int i = 0; i < model.getProblems().size(); i++){
            textView[i] = new CheckedTextView(context);
            textView[i].setText(model.getProblems().get(i));
            textView[i].setPadding(10,10,10,10);
            textView[i].setTextSize(15);
            int finalI = i;
            textView[i].setOnClickListener(v -> {
                if (textView[finalI].isChecked()){
                    textView[finalI].setChecked(false);
                    textView[finalI].setCheckMarkDrawable(0);
                }else {
                    textView[finalI].setChecked(true);
                    textView[finalI].setCheckMarkDrawable(R.drawable.checked);
                }
            });
            holder.problems.addView(textView[i]);
        }

    }

    @NonNull
    @Override
    public RecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return RecyclerHolder.buildFor(parent);
    }

    public final static class RecyclerHolder extends RecyclerView.ViewHolder {

        private static final int LAYOUT = R.layout.item_types;

        ExpansionLayout expansionLayout;
        TextView textView;
        ImageView imageView;
        LinearLayout problems;

        public static RecyclerHolder buildFor(ViewGroup viewGroup){
            return new RecyclerHolder(LayoutInflater.from(viewGroup.getContext()).inflate(LAYOUT, viewGroup, false));
        }

        public RecyclerHolder(View itemView) {
            super(itemView);
            expansionLayout = itemView.findViewById(R.id.expansionLayout);
            textView = itemView.findViewById(R.id.type_name);
            imageView = itemView.findViewById(R.id.type_image);
            problems = itemView.findViewById(R.id.problems_list);
        }

        public ExpansionLayout getExpansionLayout() {
            return expansionLayout;
        }
    }
}

创建接口 class 并将此接口绑定到适配器 class 的构造函数中。

public interface CustomDialogListener {
    void onItemClicked(CheckedTextView checkedTextView);
}

这是适配器 class

private CustomDialogListener mViewClickListener;
public ProductsAdapter(Context context,boolean status) {
    this.context = context;
    this.status = status;
    this.mViewClickListener = (CustomDialogListener) context;
}

并为您所需的操作分配接口 class 方法,例如在任何视图上单击 onClick。

 textView[i].setOnClickListener(v -> {
            if (textView[finalI].isChecked()){
                textView[finalI].setChecked(false);
                textView[finalI].setCheckMarkDrawable(0);
            }else {
                textView[finalI].setChecked(true);
                textView[finalI].setCheckMarkDrawable(R.drawable.checked);
            }
            listener.onItemClicked(textView);
        });

在父级中添加接口实现 activity class 并覆盖接口方法。

public class ParentActivity extends AppCompatActivity implements 
YourAdapter.CustomDialogListener {
  
  @Override
  public void onItemClicked(CheckedTextView checkedTextView) {
    //Log.d("Item clicked", checkedTextView.isChecked()+"");
  }
}

这就是跟踪 recyclerView 事件的方法。 希望我说清楚了。