本文介绍了使用RecyclerView.Adapter在onBindViewHolder()内设置onItemClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义对象:

Student.class

Student.class

public class Student {
  private String name;
  private String age;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAge() {
    return age;
  }

  public void setAge(String age) {
    this.age = age;
  }
}

然后我像这样实现RecyclerView.Adapter:

MyAdapter.class

MyAdapter.class

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

  private Context context;
  private ArrayList<Student> students = new ArrayList<>();

  public MyAdapter(Context mContext, ArrayList<Student> mStudents) {
    this.context = mContext;
    this.students = mStudents;
  }

  @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(context).inflate(R.layout.item_row, parent, false);
    return new ViewHolder(v);
  }

  @Override public void onBindViewHolder(ViewHolder holder, int position) {

    final Student student = students.get(position);

    holder.name.setText(student.getName());
    holder.age.setText(student.getAge());

    holder.setClickListener(new ItemClickListener() {
      @Override public void onClickItem(int pos) {
        Toast.makeText(context, "CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
      }

      @Override public void onLongClickItem(int pos) {
        Toast.makeText(context, "LONG CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
      }
    });
  }

  @Override public int getItemCount() {
    return students.size();
  }

  public static class ViewHolder extends RecyclerView.ViewHolder
      implements View.OnClickListener, View.OnLongClickListener {

    private TextView name;
    private TextView age;
    private ItemClickListener mListener;

    public ViewHolder(View itemView) {

      super(itemView);
      name = (TextView) itemView.findViewById(R.id.tv_name);
      age = (TextView) itemView.findViewById(R.id.tv_age);

      itemView.setOnClickListener(this);
      itemView.setOnLongClickListener(this);
    }

    public void setClickListener(ItemClickListener listener) {
      this.mListener = listener;
    }

    @Override public void onClick(View view) {
      mListener.onClickItem(getLayoutPosition());
    }

    @Override public boolean onLongClick(View view) {
      mListener.onLongClickItem(getLayoutPosition());
      return true;
    }
  }

  public interface ItemClickListener {
    void onClickItem(int pos);

    void onLongClickItem(int pos);
  }
}

正如您在onBindViewHolder()中看到的那样,我将onItemClickListener()设置为持有人.

As you can see in onBindViewHolder(), I set onItemClickListener() for holder.

@Override public void onBindViewHolder(ViewHolder holder, int position) {

    final Student student = students.get(position);
    holder.name.setText(student.getName());
    holder.age.setText(student.getAge());

    holder.setClickListener(new ItemClickListener() {
      @Override public void onClickItem(int pos) {
        Toast.makeText(context, "CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
      }

      @Override public void onLongClickItem(int pos) {
        Toast.makeText(context, "LONG CLICK : " + student.getName(), Toast.LENGTH_SHORT).show();
      }
    });
  }

但是我知道每次有新项目滚动到视图时都会调用onBindViewHolder()方法.因此,在onBindViewHolder()内部调用click listener会导致内部昂贵的操作.

But I know onBindViewHolder() method will be called every time a new item scrolls into view. So call click listener inside onBindViewHolder() causes expensive operations inside it.

如何解决此问题?

推荐答案

您需要在ViewHolder视图上的onClickListener()上进行设置,即您的情况下为itemView.单击完整视图即根视图(在本例中为itemView)后,它将立即调用onClick()方法

You need to set on onClickListener() on the view of the ViewHolder i.e. itemView in your case. It will call the onClick() method as soon as you click the complete view i.e. root view (itemView in your case)

您还可以在根的子级(即名称和年龄)上设置onClickListener().

you can also set onClickListener() on the children of the root i.e. name and age.

内部ViewHolder(View itemView)构造函数:

itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext,"clicked="+ getPosition(),Toast.LENGTH_SHORT).show();

                }
            });

类似地,您也可以拨打onLongClickListner().还有子视图上的name.setOnClickLisner().

Similarly you can call onLongClickListner() also.And name.setOnClickLisner() on child view too.

这篇关于使用RecyclerView.Adapter在onBindViewHolder()内设置onItemClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 07:53