Android - Recycler View onClick Listener无法正常工作

时间:2016-04-13 11:37:16

标签: android android-recyclerview onclicklistener recycler-adapter

我已经制作了一个RecyclerView适配器并使用该适配器来扩充回收器视图中的数据,我以前使用了ListView onItemClickListner的一般方法,但它似乎不能与RecyclerView一起使用,请帮助我解决这个问题< / p>

我的RecyclerView Adapter类是:

public class TimeAdaptra extends      
RecyclerView.Adapter<TimeAdaptra.RecyclerViewHolder> {
ArrayList<String> time, status;

public TimeAdaptra(ArrayList<String> title, ArrayList<String> dec) {
    this.time = title;
    this.status = dec;

}

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

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
    holder.myTitle.setText(time.get(position));
    holder.mydec.setText(status.get(position));
}

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

public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
    TextView myTitle;
    TextView mydec;

    public RecyclerViewHolder(View view) {
        super(view);
        myTitle = (TextView) view.findViewById(R.id.timee);
        mydec = (TextView) view.findViewById(R.id.status);
    }
}
}

我正在做这样的通货膨胀:

public class Slots extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager manager;
private ArrayList<String> Time = new ArrayList<String>();
private ArrayList<String> Status = new ArrayList<String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_slots);

    Time.add("18:00");
    Status.add("Available");
    recyclerView = (RecyclerView) this.findViewById(R.id.timeslice);
    manager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(manager);
    recyclerView.setHasFixedSize(true);
    adapter = new TimeAdaptra(Time, Status);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

1 个答案:

答案 0 :(得分:1)

使RecyclerViewHolder实现View.OnClickListener,如下所示:

  public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView myTitle;
        TextView mydec;

        public RecyclerViewHolder(View view) {
            super(view);
            myTitle = (TextView) view.findViewById(R.id.timee);
            mydec = (TextView) view.findViewById(R.id.status);
            myTitle.setOnClickListener(this);
            mydec.setOnClickListener(this);

        }

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.timee:
                   // do task
                    break;
                    case R.id.status:
                    //do task
                    break;
                    }
          }
    }

如果您想点击列表中的每个项目,那么您应该获得id正在膨胀的xml的根布局setOnClickListener()PasswordProfile