我做开发遇到一个多选的需求,也不是太难,挺好做的,所以就分享个大家。那就是列表的多选,不多说了,直接上代码。
基本上都是在适配器中操作。
package com.example.mvcframework.adapter;
import android.Manifest;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.mvcframework.R;
import com.example.mvcframework.activity.CultivateActivity;
import com.example.mvcframework.activity.FileDisplayActivity;
import com.example.mvcframework.entity.CultivateEntity;
import com.example.mvcframework.entity.Test_Tree_Assart;
import com.example.mvcframework.utils.Urls;
import java.util.ArrayList;
import java.util.List;
import pub.devrel.easypermissions.EasyPermissions;
public class Exame_Ti_Adapter extends RecyclerView.Adapter{
private CultivateActivity context;
private List children;
private int type;
private boolean isselect;
private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
public Exame_Ti_Adapter( CultivateActivity context,List children){
this.context=context;
this.children=children;
}
public void setType(int type){
this.type=type;
notifyDataSetChanged();
}
public void isSelect(boolean isselcet){
this.isselect=isselcet;
notifyDataSetChanged();
}
@Override
public Selects_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_wdxz_textlayout,parent,false);
Selects_ViewHolder viewHolder =new Selects_ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(Selects_ViewHolder holder, int position) {
CultivateEntity.DataBean.ChildrenBean childrenBean = children.get(position);
holder.text_jtsz.setText(childrenBean.getOB_Name());
holder.tvJs.setText("讲师:"+ childrenBean.getOB_lecturer());
holder.tvlx.setText("类型:"+childrenBean.getBOD_Name());
int i = childrenBean.getCreateDateTime().indexOf(" ");
String substring = childrenBean.getCreateDateTime().substring(0, i);
holder.show_dn_time.setText(substring);
Glide.with(context).load(Urls.DZ+childrenBean.getOB_Phone()).crossFade().placeholder(R.mipmap.base_map).into(holder.iv_icon);
if (isselect==true){
holder.rb_option1.setChecked(true);
setItemChecked(position,true);
boolean b = mSelectedPositions.get(position);
Log.d("blloen", "onBindViewHolder: "+b);
}else{
holder.rb_option1.setChecked(isItemChecked(position));
}
if (type==0){
holder.rb_option1.setVisibility(View.GONE);
}else{
holder.rb_option1.setVisibility(View.VISIBLE);
}
holder.rb_option1.setOnClickListener(view -> {
if (isItemChecked(position)) {
setItemChecked(position, false);
holder.rb_option1.setChecked(false);
} else {
setItemChecked(position, true);
holder.rb_option1.setChecked(true);
}
});
holder.buttonCk.setOnClickListener(view -> {
String[] perms = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (!EasyPermissions.hasPermissions(context, perms)) {
EasyPermissions.requestPermissions(context, "需要访问手机存储权限!", 10086, perms);
} else {
FileDisplayActivity.show(context, Urls.DZ+children.get(position).getOB_Pdf());
}
});
}
@Override
public int getItemCount() {
return children.size();
}
//根据位置判断条目是否选中
private boolean isItemChecked(int position) {
return mSelectedPositions.get(position);
}
//设置给定位置条目的选择状态
private void setItemChecked(int position, boolean isChecked) {
mSelectedPositions.put(position, isChecked);
}
public List getSelectedItem() {
ListselectList=new ArrayList<>();
for (int i = 0; i < children.size(); i++) {
if (isItemChecked(i)) {
selectList.add(children.get(i).getID());
}
}
return selectList;
}
}
class Selects_ViewHolder extends RecyclerView.ViewHolder{
public RadioButton rb_option1;
public ImageView iv_icon;
public TextView text_jtsz,tvlx,tvJs,show_dn_time;
public Button buttonCk;
public Selects_ViewHolder(View itemView) {
super(itemView);
rb_option1 = (RadioButton) itemView.findViewById(R.id.radio);
iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
text_jtsz= (TextView) itemView.findViewById(R.id.text_jtsz);
tvlx= (TextView) itemView.findViewById(R.id.tvlx);
tvJs= (TextView) itemView.findViewById(R.id.tvJs);
buttonCk= (Button) itemView.findViewById(R.id.buttonCk);
show_dn_time= (TextView) itemView.findViewById(R.id.show_dn_time);//下载时间
}
}
最后就是通过adapter.getSelectedItem()。获取到一个集合。完成!