RecyclerView 多选 删除

效果如图,多选框默认隐藏,长按显示。  本例加入了第三方库

material-cab 

就是长按后toolbar弹出的菜单。


首先定义布局activity_main.xml,主要是 RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.gao.myobser.MainActivity">

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentTop="true"
            android:background="@color/colorPrimary">

        </android.support.v7.widget.Toolbar>

        <ViewStub
            android:id="@+id/cab_stub"
            android:layout_width="match_parent"
            android:layout_height="48dp" />
    </FrameLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/frame"/>
</RelativeLayout>


item布局,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_root"
    android:orientation="horizontal"
    android:background="@drawable/list_selector"
    android:layout_width="match_parent"
    android:layout_height="32dp">
    <CheckBox
        android:id="@+id/item_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/item_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</LinearLayout>


adapter文件


isShow判断checkbox是否显示,两个list,一个用来传数据,另一个用来判断该位置的checkbox是否被选中。

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.PlateViewHolder> {

    private List<String> list;
    private List<Boolean> listCheck;
    private Context context;
    boolean isShow=false;

    public RecyclerAdapter(List<String> list, Context context, List<Boolean> listCheck){
        this.list=list;
        this.context=context;
        this.listCheck=listCheck;
    }


    @Override
    public PlateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        return new PlateViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PlateViewHolder holder, int position) {
        holder.position=position;
        holder.textView.setText(position+"");
        holder.checkBox.setChecked(listCheck.get(position));
        if(isShow){
            holder.checkBox.setVisibility(View.VISIBLE);
        }else {
            holder.checkBox.setVisibility(View.GONE);
        }
    }

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

    public class PlateViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener,View.OnClickListener,CompoundButton.OnCheckedChangeListener{
        private TextView textView;
        private CheckBox checkBox;
        private LinearLayout rootView;
        private int position;
        public PlateViewHolder(View itemView) {
            super(itemView);
            rootView= (LinearLayout) itemView.findViewById(R.id.item_root);
            textView= (TextView) itemView.findViewById(R.id.item_text);
            checkBox= (CheckBox) itemView.findViewById(R.id.item_checkbox);

            checkBox.setOnCheckedChangeListener(this);
            rootView.setOnClickListener(this);
            rootView.setOnLongClickListener(this);
        }

        @Override
        public boolean onLongClick(View v) {
            if(onItemClickListener!=null){
                return onItemClickListener.setOnItemLongClick(position);
            }
            return false;
        }

        @Override
        public void onClick(View v) {
            if(onItemClickListener!=null){
                if(checkBox.isChecked()){
                    checkBox.setChecked(false);
                    onItemClickListener.setOnItemClick(position,false);
                }else {
                    checkBox.setChecked(true);
                    onItemClickListener.setOnItemClick(position,true);
                }
            }
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(onItemClickListener!=null){
                onItemClickListener.setOnItemCheckedChanged(position,isChecked);
            }
        }
    }

    public interface OnItemClickListener {
        void setOnItemClick(int position, boolean isCheck);
        boolean setOnItemLongClick(int position);
        void setOnItemCheckedChanged(int position, boolean isCheck);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
}


MainActivity 文件

public class MainActivity extends AppCompatActivity implements MaterialCab.Callback {
    private RecyclerView recyclerView;
    private List<Boolean> listCheck;
    private MaterialCab mCab;
    RecyclerAdapter adapter;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new SlideInOutRightItemAnimator(recyclerView));
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL));
        recyclerView.setHasFixedSize(true);
        list = new ArrayList<>();
        listCheck = new ArrayList<>();
//list listCheck 赋值
 for (int i = 0; i < 40; i++) {
            list.add(i + "");
            listCheck.add(false);
        }

        adapter = new RecyclerAdapter(list, this, listCheck);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemListener(new RecyclerAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClick(int position, boolean isCheck) {
                listCheck.set(position, isCheck);
            }

            @Override
            public boolean setOnItemLongClick(int position) {

                if (mCab == null)
                    mCab = new MaterialCab(MainActivity.this, R.id.cab_stub).setMenu(R.menu.menu_cab).start(MainActivity.this);
                else if (!mCab.isActive())
                    mCab.reset().setMenu(R.menu.menu_cab).start(MainActivity.this);
                adapter.isShow = true;//长按改变checkbox显示状态

                adapter.notifyDataSetChanged();
                return true;
            }

            @Override
            public void setOnItemCheckedChanged(int position, boolean isCheck) {
                listCheck.set(position,isCheck);
            }

        });
    }


    @Override
    public boolean onCabCreated(MaterialCab cab, Menu menu) {
//长按弹出顶部的图标,此时将checkbox状态全部置为false
 for (int i = 0; i < listCheck.size(); i++) {
            listCheck.set(i, false);
        }
        return true;
    }

    @Override
    public boolean onCabItemClicked(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.menu_delete:
                int length=listCheck.size();
                for (int i = length-1; i>=0; i--) {
                    if (listCheck.get(i)) {
                        list.remove(i);
                        listCheck.remove(i);
                        adapter.notifyItemRemoved(i);
                        adapter.notifyItemRangeChanged(0,list.size());
                    }
                }
                break;
            case R.id.menu_select_all:
                for (int i = 0; i < listCheck.size(); i++) {
                    listCheck.set(i, true);
                }
                adapter.notifyDataSetChanged();
                break;
        }

        return true;
    }

    @Override
    public boolean onCabFinished(MaterialCab cab) {
        adapter.isShow = false;
        adapter.notifyDataSetChanged();
        return true;
    }

    @Override
    public void onBackPressed() {
        if (mCab != null && mCab.isActive()) {
            mCab.finish();
            mCab = null;
        } else {
            super.onBackPressed();
        }
    }
}

代码已上传。http://download.csdn.net/detail/u014290233/9585430



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值