如何为RecyclerView设置onItemClickListener?

时间:2016-12-17 16:51:12

标签: android android-recyclerview onitemclicklistener

我正在尝试实施recyclerView但是如何为我的recyclerView设置onItemClickListener?

2 个答案:

答案 0 :(得分:13)

在Android中, RecyclerView 在ListView中没有任何listeners。因此,您需要通过扩展RecyclerView.OnItemTouchListener来创建自己的类。

创建课程RecyclerTouchListener.java

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

  private GestureDetector gestureDetector;
  private ClickListener clickListener;

public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
    this.clickListener = clickListener;
    gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {

            return true;

        }

        @Override
        public void onLongPress(MotionEvent e) {

            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());

            if (child != null && clickListener != null) {

                clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));

            }
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

    View child = rv.findChildViewUnder(e.getX(), e.getY());

    if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {

        clickListener.onClick(child, rv.getChildAdapterPosition(child));

    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

public interface ClickListener {

    void onClick(View view, int position);

    void onLongClick(View view, int position);
   }
 }

然后在活动中,覆盖onClick方法。

 recyclerView.addOnItemTouchListener(new RecyclerTouchListener(context, recyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {

         //   Toast.makeText(getActivity(), position+ " is selected successfully", Toast.LENGTH_SHORT).show();

                //handle click event

        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

通过这种方式,您可以处理recyclerview item click event

答案 1 :(得分:3)

使用此代码

定义名为RecyclerItemClickListener的类
class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

    interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private GestureDetector mGestureDetector;

    RecyclerItemClickListener(Context context, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
}

然后在您的活动中使用此代码

recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(MainActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
    }
}));