RecyclerView点击事件

//适配器中定义为public


//点击事件

        recyclerview.addOnItemTouchListener(new OnRecyclerItemClickListener(recyclerview) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder) {
                MyAdapter.MyViewHolder adapter= (MyAdapter.MyViewHolder) viewHolder;
                String string = adapter.textView.getText().toString();
                Toast.makeText(MainActivity.this, "点击"+string, Toast.LENGTH_SHORT).show();
            }


            @Override
            public void onLongClick(RecyclerView.ViewHolder viewHolder) {
                Toast.makeText(MainActivity.this, "长按", Toast.LENGTH_SHORT).show();
            }

        });


package com.jcodecraeer.xrecyclerview.click;


import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;


/*******************************************************************
 *    * * * *   * * * *   *     *       Created by OCN.Yang
 *    *     *   *         * *   *       Time:2017/8/2 10:30.
 *    *     *   *         *   * *       Email address:[email protected]
 *    * * * *   * * * *   *     *.Yang  Web site:www.ocnyang.com
 *******************************************************************/




public abstract class OnRecyclerItemClickListener implements RecyclerView.OnItemTouchListener{
    private GestureDetectorCompat mGestureDetectorCompat;
    private RecyclerView mRecyclerView;


    public OnRecyclerItemClickListener(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        mGestureDetectorCompat = new GestureDetectorCompat(mRecyclerView.getContext(),
                new ItemTouchHelperGestureListener());
    }


    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        mGestureDetectorCompat.onTouchEvent(e);
        return false;
    }


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


    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {


    }


    public abstract void onItemClick(RecyclerView.ViewHolder viewHolder);
    public abstract void onLongClick(RecyclerView.ViewHolder viewHolder);


    private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener{
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            View childViewUnder = mRecyclerView.findChildViewUnder(e.getX(), e.getY());
            if (childViewUnder != null) {
                RecyclerView.ViewHolder childViewHolder = mRecyclerView.getChildViewHolder(childViewUnder);
                onItemClick(childViewHolder);
            }
            return true;
        }


        @Override
        public void onLongPress(MotionEvent e) {
            View childViewUnder = mRecyclerView.findChildViewUnder(e.getX(), e.getY());
            if (childViewUnder != null) {
                RecyclerView.ViewHolder childViewHolder = mRecyclerView.getChildViewHolder(childViewUnder);
                onLongClick(childViewHolder);
            }
        }
    }
}

你可能感兴趣的:(RecyclerView点击事件)