RecyclerView的item点击事件和长按事件

时间:2022-10-12 22:32:25

RecyclerView 是Android L版本中新添加的一个用来取代ListView的SDK,灵活性很强,可以实现listview,gridview,瀑布流的效果,但是没有专门封装的item点击事件和长按事件,个人一般在适配器里写回调实现这俩方法

首先要,先引入包来支持控件

compile 'com.android.support:recyclerview-v7:22.2.1'

然后要在适配器里写好回调

public class PresetAdapter extends RecyclerView.Adapter<AlarmPresetAdapter.MyViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private OnRecyclerItemLongListener mOnItemLong = null;
    private ArrayList<String> arrayList;

    public PresetAdapter(Context context,ArrayList<String> arrayList){
        this.context = context;
        this.arrayList = arrayList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_preset, parent, false);
        MyViewHolder holder = new MyViewHolder(view, mOnItemClickListener,mOnItemLong);
// view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv_alarm_preset_type.setText(arrayList.get(position));
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {
        private OnRecyclerViewItemClickListener mOnItemClickListener = null;
        private OnRecyclerItemLongListener mOnItemLong = null;
        private TextView tv_alarm_preset_value;
        private TextView tv_alarm_preset_type;
        private TextView tv_alarm_preset_status;
        private SwitchButton sb_alarm_preset_switch;

        public MyViewHolder(View itemView, OnRecyclerViewItemClickListener mListener,OnRecyclerItemLongListener longListener) {
            super(itemView);
            this.mOnItemClickListener = mListener;
            this.mOnItemLong = longListener;
            tv_alarm_preset_value = (TextView) itemView.findViewById(R.id.tv_alarm_preset_value);
            tv_alarm_preset_type = (TextView) itemView.findViewById(R.id.tv_alarm_preset_type);
            tv_alarm_preset_status = (TextView) itemView.findViewById(R.id.tv_alarm_preset_status);
            sb_alarm_preset_switch = (SwitchButton) itemView.findViewById(R.id.sb_alarm_preset_switch);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                //注意这里使用getTag方法获取数据
                mOnItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if(mOnItemLong != null){
                mOnItemLong.onItemLongClick(v,getPosition());
            }
            return true;
        }
    }

    //define interface
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int data);

    }
    public interface OnRecyclerItemLongListener{
        void onItemLongClick(View view,int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    public void setOnItemLongClickListener(OnRecyclerItemLongListener listener){
        this.mOnItemLong =  listener;
    }

可以看到无论是item点击事件还是长按事件都是写在MyViewHolder上,来实现的

最后还要在Activity里实现这两个方法

adapter.setOnItemClickListener(new AlarmPresetAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int data) {
                T.showShort(context, "点击了");
            }
        });
        adapter.setOnItemLongClickListener(new AlarmPresetAdapter.OnRecyclerItemLongListener() {
            @Override
            public void onItemLongClick(View view, int position) {
                T.showShort(context, "长按了");
            }
        });