为你的RecyclerView的item点击事件

前言

本文用作个人笔记,也供同行参考,共同进步。

正题

    我们都知道RecyclerView的强大,替代listView,gridView也能实现瀑布流效果等。个人习惯用新控件,不断进步。所以在我需要listView的情况下我就会使用RecyclerView。简单说下使用步骤。

进入栗子环节

//和listView使用步骤相同,初始化控件,数据源
    @BindView(R.id.rv) RecyclerView mRecyclerView;
    String[] datas = {"个人资料","消息推送","帐号管理","连接健康","帮助与反馈","清空缓存"};
//初始化适配器,RecyclerView的适配器要重写RecyclerView.Adapter
//适配器的重写是重点,这里先略过。
    adpter = mAppWorker.initAdapter(datas);
//与ListView不同的步骤来了,为你的RecyclerView添加布局管理
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//最后一个步骤
    mRecyclerView.setAdapter(adpter);

重点

ListView lv = null;

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {

            }
        });
RecyclerView并不能设置OnItemClickListener。不过没关系,山不就我,我去就山。我们可以为RecyclerView的每个item设置点击事件。

先来看看RecyclerView专用适配器的结构

public class SettingAdapter extends RecyclerView.Adapter<SettingAdapter.MyViewHolder>{

        @Override
    public SettingAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //初始化布局文件
          View v = LayoutInflater.from(context).inflate(R.layout.x, parent, false);
          //在onCreateViewHolder方法中设置holder
      SettingAdapter.MyViewHolder holder = new SettingAdapter.MyViewHolder(v);
            return holder;
        }

        @Override
        public void onBindViewHolder(SettingAdapter.MyViewHolder holder, int position) {
            //在onBindViewHolder做操作
            holder.tv1.setText("test");
        }

        @Override
        public int getItemCount() {
        //返回数据源长度
            return datas.length;
        }
        public class MyViewHolder extends RecyclerView.ViewHolder {

         @BindView(R.id.recyclerview_tv1) TextView tv1;
            public MyViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this,itemView);
            }
        }
    }
从适配器上入手,我们应该在onCreateViewHolder方法中为每个布局(item)设置点击事件就可以了。但在设置点击事件的时候,我们应该知道 item的position,所以我们可以通过在onBindViewHolder绑定方法中 对对应的item设置 tag属性来区别。

举个栗子

    第一步,创建接口,让适配器实现,并重写
public interface OnRecyclerItemClick {
//回调方法,view是点击的item布局,tagPois就是item的position
    void onItemClick(View view , int tagPois);
}
//重写onItemClick
 @Override
        public void onItemClick(View view, int tag) {
            Intent itent = null;
            switch(tag){
                case 0:
                  itent = new Intent(instance,PersonalInfoSetActivity.class);
                  break;
            }
            if (itent!=null){
                instance.startActivity(itent);
            }else {
                Toast.makeText(context, "还没设置点击事件   item"+tag, Toast.LENGTH_SHORT).show();
            }
        }
    第二步,设置tag
@Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            //设置tag为position
            holder.itemView.setTag(position);
        }
    第三步,设置点击事件
  @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
           //设置点击事件
           View v = LayoutInflater.from(context).inflate(R.layout.personal_setting_recyclerview, parent, false);

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClick(v,(int) v.getTag());
                }
            });
        }
流程就是这样了,最后附上个人完整代码
public class SettingAdapter extends RecyclerView.Adapter<SettingAdapter.MyViewHolder> implements OnRecyclerItemClick{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(context).inflate(R.layout.personal_setting_recyclerview, parent, false);
            MyViewHolder holder = new MyViewHolder(v);
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClick(v,(int) v.getTag());
                }
            });
            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            //设置tag为position
            holder.itemView.setTag(position);

            holder.tv1.setText(datas[position]);
            if (position==0){
                holder.tv2.setVisibility(View.VISIBLE);
                holder.tv2.setText("修改个人资料");
                holder.iv1.setVisibility(View.VISIBLE);
                //1.头像的加载

            }
            if (position==1){
                holder.tv2.setVisibility(View.VISIBLE);
                holder.tv2.setText("评论,+关注,系统通知开关");
                holder.iv2.setVisibility(View.INVISIBLE);
                holder.tb.setVisibility(View.VISIBLE);
                holder.tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        //通知开关
                        if (isChecked){
                            Toast.makeText(context, "打开通知", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(context, "关闭通知", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
            if (position==datas.length-1){
                holder.tv3.setVisibility(View.VISIBLE);
//                缓存
                holder.tv3.setText(CacheMannger.getCacheSize(context));
            }
        }

        @Override
        public int getItemCount() {
            return datas.length;
        }

        @Override
        public void onItemClick(View view, int tag) {
            Intent itent = null;
            switch(tag){
                case 0:
                  itent = new Intent(instance,PersonalInfoSetActivity.class);
                  break;

                case 1:

                  break;

                case 2:

                  break;

                case 3:

                  break;

                case 4:

                  break;

                case 5:

                  break;
            }
            if (itent!=null){
                instance.startActivity(itent);
            }else {
                Toast.makeText(context, "还没设置点击事件   item"+tag, Toast.LENGTH_SHORT).show();
            }
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.recyclerview_tv1) TextView tv1;
            @BindView(R.id.recyclerview_tv2) TextView tv2;
            @BindView(R.id.recyclerview_tv3) TextView tv3;
            @BindView(R.id.recyclerview_iv1) ImageView iv1;
            @BindView(R.id.recyclerview_iv2) ImageView iv2;
            @BindView(R.id.toggleButton) ToggleButton tb;


            public MyViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this,itemView);
           }
       }
    }

你可能感兴趣的:(Android开发)