zoukankan      html  css  js  c++  java
  • 解决RecyclerView的onClickListener问题

    在MainActivity中添加:

            mRecyclerView.addOnItemTouchListener(
                    new RecyclerItemClickListener(context, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            // do whatever
                            mDrawerLayout.closeDrawers();
    
                        }
    
                        @Override
                        public void onItemLongClick(View view, int position) {
                            // do whatever
                            mDrawerLayout.closeDrawers();
    
                        }
                    })
            );

    添加Java文件:

    RecyclerItemClickListener.java

    package com.whisper.myapplication;
    
    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    import android.view.View;
    
    public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener
    {
        public static interface OnItemClickListener
        {
            public void onItemClick(View view, int position);
            public void onItemLongClick(View view, int position);
        }
    
        private OnItemClickListener mListener;
        private GestureDetector mGestureDetector;
    
        public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener)
        {
            mListener = listener;
    
            mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener()
            {
                @Override
                public boolean onSingleTapUp(MotionEvent e)
                {
                    return true;
                }
    
                @Override
                public void onLongPress(MotionEvent e)
                {
                    View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
    
                    if(childView != null && mListener != null)
                    {
                        mListener.onItemLongClick(childView, recyclerView.getChildPosition(childView));
                    }
                }
            });
        }
    
        @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.getChildPosition(childView));
            }
    
            return false;
        }
    
        @Override
        public void onTouchEvent(RecyclerView view, MotionEvent motionEvent){}
    }

    参考文章:

    RecyclerView onClick

  • 相关阅读:
    解决 Windows 资源管理器 CPU 占用率很高的问题
    彻底卸载Cygwin
    Cygwin 快速镜像站点
    解决 cygwin 在 win10 下中文显示乱码的问题
    酷派大神 F1 连接调试,日志获取的方法
    陌路
    游戏开发中自定义脚本与配置
    在 cygwin 的环境下调用 libgdx gdx-tools
    在 cygwin 的环境下使用 git 的时候中文编码的问题
    对 android apk 进行重新签名操作
  • 原文地址:https://www.cnblogs.com/wkun/p/4378129.html
Copyright © 2011-2022 走看看