RecycleView设置点击事件On ——>内部接口OnItemTouchListener

新建RecyclerViewClickListener2.java:

public class RecyclerViewClickListener2 implements RecyclerView.OnItemTouchListener {

    private GestureDetector mGestureDetector;
    private OnItemClickListener mListener;

    //内部接口,定义点击方法以及长按方法
    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public RecyclerViewClickListener2(Context context, final RecyclerView recyclerView,OnItemClickListener listener){
        mListener = listener;
        mGestureDetector = new GestureDetector(context,
                new GestureDetector.SimpleOnGestureListener(){ //这里选择SimpleOnGestureListener实现类,可以根据需要选择重写的方法
                    //单击事件
                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        View childView = recyclerView.findChildViewUnder(e.getX(),e.getY());
                        if(childView != null && mListener != null){
                            mListener.onItemClick(childView,recyclerView.getChildLayoutPosition(childView));
                            return true;
                        }
                        return false;
                    }
                    //长按事件
                    @Override
                    public void onLongPress(MotionEvent e) {
                        View childView = recyclerView.findChildViewUnder(e.getX(),e.getY());
                        if(childView != null && mListener != null){
                            mListener.onItemLongClick(childView,recyclerView.getChildLayoutPosition(childView));
                        }
                    }
                });
    }
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        //把事件交给GestureDetector处理
        if(mGestureDetector.onTouchEvent(e)){
            return true;
        }else
            return false;
    }

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

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

在MainActivity.java改动如下:

mRecyclerView.addOnItemTouchListener(new RecyclerViewClickListener2(this, mRecyclerView,
        new RecyclerViewClickListener2.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(MainActivity.this,"Click "+mData.get(position),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemLongClick(View view, int position) {
        Toast.makeText(MainActivity.this,"Long Click "+mData.get(position),Toast.LENGTH_SHORT).show();
    }
}));
转载自:https://www.jianshu.com/p/f2e0463e5aef
要实现RecycleView嵌套RecycleView点击事件,需要在Adapter中设置点击事件监听器,并将其传递给子RecycleView的Adapter。 首先,在父RecycleView的Adapter中,实现一个接口来处理子RecycleView点击事件。例如: ``` public interface OnChildClickListener { void onChildClick(View view, int parentPosition, int childPosition); } ``` 然后,在父RecycleView的Adapter中,设置一个OnChildClickListener对象,并在点击事件中调用它: ``` public class ParentAdapter extends RecyclerView.Adapter<ParentAdapter.ViewHolder> { private ArrayList<ArrayList<String>> mData; private OnChildClickListener mChildClickListener; public ParentAdapter(ArrayList<ArrayList<String>> data) { mData = data; } public void setOnChildClickListener(OnChildClickListener listener) { mChildClickListener = listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.parent_item, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { // set the data for each item in the view holder ArrayList<String> childData = mData.get(position); ChildAdapter childAdapter = new ChildAdapter(childData); childAdapter.setOnChildClickListener(new ChildAdapter.OnChildClickListener() { @Override public void onChildClick(View view, int childPosition) { // pass the click event to the parent adapter if (mChildClickListener != null) { mChildClickListener.onChildClick(view, position, childPosition); } } }); holder.childRecyclerView.setAdapter(childAdapter); } public static class ViewHolder extends RecyclerView.ViewHolder { RecyclerView childRecyclerView; public ViewHolder(View v) { super(v); childRecyclerView = (RecyclerView) v.findViewById(R.id.child_recycler_view); } } } ``` 在上面的代码中,我们在onBindViewHolder()方法中创建一个ChildAdapter,并将其设置为子RecycleView的Adapter。同时,我们还设置了一个OnChildClickListener对象,并在ChildAdapter中调用它来处理子RecycleView点击事件。 接下来,我们需要在ChildAdapter中设置一个OnChildClickListener对象,并在点击事件中调用它: ``` public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ViewHolder> { private ArrayList<String> mData; private OnChildClickListener mChildClickListener; public ChildAdapter(ArrayList<String> data) { mData = data; } public void setOnChildClickListener(OnChildClickListener listener) { mChildClickListener = listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.child_item, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { // set the data for each item in the view holder String item = mData.get(position); holder.textView.setText(item); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // pass the click event to the parent adapter if (mChildClickListener != null) { mChildClickListener.onChildClick(view, position); } } }); } public static class ViewHolder extends RecyclerView.ViewHolder { TextView textView; public ViewHolder(View v) { super(v); textView = (TextView) v.findViewById(R.id.text_view); } } public interface OnChildClickListener { void onChildClick(View view, int childPosition); } } ``` 在上面的代码中,我们在onBindViewHolder()方法中设置了一个点击事件监听器,并在点击事件中调用了OnChildClickListener对象来处理点击事件。 最后,在Activity或Fragment中,我们需要设置ParentAdapter的OnChildClickListener对象,并在回调方法中处理点击事件: ``` parentAdapter.setOnChildClickListener(new ParentAdapter.OnChildClickListener() { @Override public void onChildClick(View view, int parentPosition, int childPosition) { // handle the child item click event here } }); ``` 在上面的代码中,我们设置了一个ParentAdapter的OnChildClickListener对象,并在回调方法中处理子RecycleView点击事件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值