码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView的2种监听方式

时间:2015-06-30 16:28:20      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:recycler   监听   事件   

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 + "");
         }
    }));

版权声明:本文为博主原创文章,未经博主允许不得转载。

RecyclerView的2种监听方式

标签:recycler   监听   事件   

原文地址:http://blog.csdn.net/pengkv/article/details/46696111

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!