zoukankan      html  css  js  c++  java
  • RecyclerView的2种监听方式

    NO.1:在自己定义适配器的ViewHolder里面写监听事件

     //RecyclerView适配器
        public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
            private Context mContext;
            private LayoutInflater mLayoutInflater;
            private String[] mCityName;
    
            public RecyclerViewAdapter(Context mContext) {
                this.mContext = mContext;
                mLayoutInflater = LayoutInflater.from(mContext);
                mCityName = mContext.getResources().getStringArray(R.array.city_name);
            }
    
            //创建新ViewHolder,被LayoutManager所调用
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new RecyclerViewViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
            }
    
            //将数据与界面进行绑定的操作
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                ((RecyclerViewViewHolder) holder).mTextView.setText(mCityName[position]);
            }
    
            //获取数据的数量
            @Override
            public int getItemCount() {
                return mCityName == null ? 0 : mCityName.length;
            }
    
            //自己定义的ViewHolder,持有每一个Item的的全部界面元素
            public class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
                TextView mTextView;
    
                public RecyclerViewViewHolder(View itemView) {
                    super(itemView);
                    mTextView = (TextView) itemView.findViewById(R.id.tv_city);
                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ToastUtil.show(mContext, getPosition() + "");
                        }
                    });
                }
            }
        }

    这里写图片描写叙述

    NO.2:封装个监听类

    public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
        private OnItemClickListener mListener;
    
        public interface OnItemClickListener {
            public void onItemClick(View view, int position);
        }
    
        GestureDetector mGestureDetector;
    
        public 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.getChildPosition(childView));
                return true;
            }
            return false;
        }
    
        @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
    }
    

    用法:

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
             ToastUtil.show(OneActivity.this, position + "");
             }
        }));
  • 相关阅读:
    C#数组学习
    关于servlet中要写初始化逻辑应该重载有参还是无参的init
    servlet初识servletConfig
    servlet通过响应头Content-Disposition实现文件下载效果
    response中setCharacterEncoding和setContentType的区别
    通过refresh响应头,定时刷新或隔n秒跳转页面
    通过location响应头实现重定向
    三层交换原理
    什么是CLOS架构?
    Cat8 八类网线是什么?与Cat5、Cat6、Cat7网线的区别?
  • 原文地址:https://www.cnblogs.com/llguanli/p/7338816.html
Copyright © 2011-2022 走看看