Recyclerview 实现多选,单选,全选,反选,批量删除的步骤
1.在Recyclerview布局中添加上底部的全选和反选按钮,删除按钮,和计算数量等控件
2.这里选中的控件没有用checkbox来做,用的是imageview,选中和不选中其实是两张图片
3.默认是不显示选中的控件的,点击编辑的时候显示,点击取消的时候隐藏
4.通过adapter和activity数据之间的传递,然后进行具体的操作
具体代码如下:
在recyclerview的布局中写全选,反选,删除,计数等相应的控件
//清单文件删除标题 ,换成返回按钮
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
//对应的主布局
//对应的解析的布局
对应的适配器
public class HomeAdaper extends RecyclerView.Adapter {
Context context;
List song_list;
// 创建一个map集合 用来保存checkbox选中状态
public HashMap isSelected;
public HomeAdaper(Context context, List 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 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 song_list) {
Log.d("mylog", "getNews: 进来了" + song_list.get(0).getTitle());
recycler.setAdapter(homeadper =new HomeAdaper(MainActivity.this,song_list));
}
}