回收视图OnitemclickListener

时间:2014-12-28 09:43:40

标签: android view

如何处理项目RecycleView的点击? 我找到了在ViewHolder中编写适配器的方法, 但我如何处理这些相同的点击活动,

 public ViewHolder(View itemView) {         
itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("TAG", "Element " + getPosition() + " clicked.");
                }
            });

我试过

  AdapterViewCompat.OnItemClickListener clickListener = new AdapterViewCompat.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterViewCompat<?> adapterViewCompat, View view, int i, long l) {
          Toast.makeText(getActivity(), "You click " + i, Toast.LENGTH_LONG);
      }
  };
  recycleView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Toast.makeText(getActivity(), "You click now", Toast.LENGTH_LONG);
          Log.i("Screen Two ", v.getVerticalScrollbarPosition() + "");
      }
  });
    recycleView.setOnClickListener((View.OnClickListener) clickListener);
  recycleView.setOnLongClickListener(new View.OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
          return false;
      }
  });

但它们不起作用,尽管在Activity中 帮助我。

1 个答案:

答案 0 :(得分:3)

您必须为适配器创建自定义侦听器,以将click事件传播到您的活动。 OnItemClickListener中没有RecyclerView这样的内容。