RecyclerView单选框删除条目,全选删除,Recyclerview 实现多选,单选,全选,反选,批量删除的步骤 1.在Recyclerview布局中添加上底部的全选和反选按钮,删除按钮,和计算


Recyclerview 实现多选,单选,全选,反选,批量删除的步骤

1.在Recyclerview布局中添加上底部的全选和反选按钮,删除按钮,和计算数量等控件 

2.这里选中的控件没有用checkbox来做,用的是imageview,选中和不选中其实是两张图片 

3.默认是不显示选中的控件的,点击编辑的时候显示,点击取消的时候隐藏 

4.通过adapter和activity数据之间的传递,然后进行具体的操作

具体代码如下:

在recyclerview的布局中写全选,反选,删除,计数等相应的控件



//清单文件删除标题 ,换成返回按钮
  android:theme="@style/Theme.AppCompat.Light.NoActionBar">



//对应的主布局
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.eightgroup.wlb20171106.MainActivity">

    <RelativeLayout
        android:id="@+id/one"
        android:background="#09f"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <Button
            android:text="返回"
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:textSize="32sp"
            android:text="我的收藏"
            android:textColor="#fff"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:textColor="#fff"
            android:textSize="32sp"
            android:layout_alignParentRight="true"
            android:text="编辑"
            android:id="@+id/text_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <HorizontalScrollView
     android:id="@+id/hor"
        android:layout_below="@+id/one"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content">
     <LinearLayout
         android:orientation="horizontal"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">
         <TextView
             android:text="商品"
             android:textSize="30dp"
             android:textColor="@color/colorAccent"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             />
         <TextView
             android:text="路线"
             android:textSize="30dp"
             android:layout_marginLeft="100dp"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             />
     </LinearLayout>
 </HorizontalScrollView>
<android.support.v7.widget.RecyclerView
    android:layout_below="@+id/hor"
    android:id="@+id/recycler"
    android:layout_width="wrap_content"
    android:layout_height="600dp">
</android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:layout_below="@+id/recycler"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/qb"

            android:text="全选"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button

            android:id="@+id/shanchu"


            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"
            />
    </LinearLayout>

</RelativeLayout>

//对应的解析的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:fresco="http://schemas.android.com/apk/res-auto">
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        fresco:placeholderImage="@mipmap/ic_launcher"
        fresco:placeholderImageScaleType="focusCrop"
        fresco:roundAsCircle="true"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
对应的适配器
public class HomeAdaper extends RecyclerView.Adapter {
    Context context;
    List<Newss.SongListBean> song_list;
    // 创建一个map集合 用来保存checkbox选中状态
     public HashMap<Integer,Boolean> isSelected;


    public HomeAdaper(Context context, List<Newss.SongListBean> song_list) {
        this.context = context;
        this.song_list = song_list;
        isSelected = new HashMap<>();
        addisSeleted();
    }

    private void addisSeleted() {
        for (int i = 0 ; i < song_list.size();i++){
            isSelected.put(i,false);
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        MyViewHolder holder1 = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.listview, parent,
                false));
        return holder1;


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {


        MyViewHolder holder1 = (MyViewHolder) holder;
        holder1.tv.setText(song_list.get(position).getTitle());


        holder1.draweeView1.setImageURI(song_list.get(position).getPic_big());
        //创建DraweeController
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                //重试之后要加载的图片URI地址
                .setUri(song_list.get(position).getPic_big())
                //设置点击重试是否开启
                .setTapToRetryEnabled(true)
                //动画播放
                .setAutoPlayAnimations(true)
                //设置旧的Controller
                .setOldController(holder1.draweeView1.getController())
                //构建
                .build();
        //设置DraweeController
        holder1.draweeView1.setController(controller);

        // 设置checkbox的选中状态
        holder1.qb.setChecked(isSelected.get(position));
        holder1.qb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                // 每次将条目的checkbox状态存入hashmap
                isSelected.put(position,b);

            }
        });


    }


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

    class MyViewHolder extends RecyclerView.ViewHolder {


        SimpleDraweeView draweeView1;
        TextView tv;
        CheckBox qb;
        public MyViewHolder(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.tv);
            draweeView1 = (SimpleDraweeView) view.findViewById(R.id.img);
            qb = view.findViewById(R.id.checkbox);

        }


    }
}

最后删除的话就调删除的接口,遍历这个bean,判断当前的状态如果是选中的状态,就删除! 这样就OK了 !!!

//下面就是对应的主页面

public class MainActivity extends AppCompatActivity implements IView {
    RecyclerView recycler;
    HomeAdaper homeadper;
    List<Newss.SongListBean> song_list;
    UserPresenter userpresenter;
    Button  shanchu ,back;
    CheckBox qb;
     boolean isnoCheckAll = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        shanchu = (Button)findViewById(R.id.shanchu);
        back = (Button)findViewById(R.id.back) ;
        qb = (CheckBox)findViewById(R.id.qb) ;
        recycler = (RecyclerView)findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this));

        userpresenter = new UserPresenter(this);
        userpresenter.getUser(Api.HOME_URL);
        qb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    isnoCheckAll = true;
                    for (int i = 0; i < homeadper.isSelected.size(); i++) {
                        homeadper.isSelected.put(i, true);
                    }
                } else {
                    for (int i = 0; i < homeadper.isSelected.size(); i++) {
                        homeadper.isSelected.put(i, false);
                    }
                    isnoCheckAll = false;
                }
                homeadper.notifyDataSetChanged();

            }
        });

        shanchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (isnoCheckAll) {
                    homeadper.song_list.clear();
                    homeadper.notifyDataSetChanged();

                } else {
                    for (int i = 0; i < homeadper.isSelected.size(); i++) {
                        if(homeadper.isSelected.get(i)==true)
                        {
                            homeadper.song_list.remove(i);

                        }
                    }
                    homeadper.notifyDataSetChanged();



                }


            }
});
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


    }
    @Override
    public void getNews(ArrayList<Newss.SongListBean> song_list) {
        Log.d("mylog", "getNews: 进来了" + song_list.get(0).getTitle());
        recycler.setAdapter(homeadper =new HomeAdaper(MainActivity.this,song_list));
    }
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值