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

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, "长按了");
            }
        });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值