如图,列表中的字符串是从服务器获取的,数量也可能随时改变,需要做成如图的样式,并且选择权限完进行提交修改
下面是返回的数据;
{
"state": 1,
-"data": [
-{
"id": 1,
"permission": "开桌",
"shopid": 306
},
-{
"id": 2,
"permission": "买单",
"shopid": 306
},
-{
"id": 3,
"permission": "帮人下单",
"shopid": 306
},
-{
"id": 4,
"permission": "帮人买单",
"shopid": 306
},
-{
"id": 5,
"permission": "折扣",
"shopid": 306
},
-{
"id": 6,
"permission": "新增产品",
"shopid": 306
},
-{
"id": 7,
"permission": "删除产品",
"shopid": 306
},
-{
"id": 8,
"permission": "修改产品",
"shopid": 306
},
-{
"id": 9,
"permission": "转台及合并",
"shopid": 306
},
-{
"id": 10,
"permission": "订单修改",
"shopid": 306
},
-{
"id": 11,
"permission": "账单修改",
"shopid": 306
},
-{
"id": 12,
"permission": "基础信息设置",
"shopid": 306
}
]
}
和服务器约定好的是要提交权限是一个string,用,分隔,比如:”1,2,12”.
首先思路是这样,
1.用一个recycleview,里面的item一个是textview另一个是checkbox,checkbox的button状态设置成这个图片选择器
2.在adapter内部设置给外面调用的监听,当选泽和选择取消的时候进行判断,选中就用StringBuilder.append这个id,选择取消就删除Stringbuilder中指定位置的id号和它后面的逗号,最后选择完成进行提交的时候删除最后一个位置的逗号.
代码:
1.adapter:
public class AccountPermissionDetailRvAdapter extends BaseQuickAdapter<PermissionBean.DataBean,BaseViewHolder>{
public AccountPermissionDetailRvAdapter(int layoutResId, @Nullable List<PermissionBean.DataBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, PermissionBean.DataBean item) {
helper.setText(R.id.tv_permission,item.getPermission());
//helper.setImageDrawable(R.id.iv_switch,mContext.getDrawable(R.drawable.iv_select_switch_button));
CheckBox mCbSwitch = helper.getView(R.id.iv_switch);
mCbSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonCheckChangeListener.onCheckChanged(isChecked,item.getId());
}
});
}
public OnSwitchButtonCheckChangeListener buttonCheckChangeListener;
public void setOnCheckBoxListener(OnSwitchButtonCheckChangeListener listener){
this.buttonCheckChangeListener = listener;
}
public interface OnSwitchButtonCheckChangeListener{
void onCheckChanged(boolean isChecked,int id);
}
}
2.获取数据成功的处理:
@Override
public void onGetPermissionSuccess(PermissionBean bean) {
List<PermissionBean.DataBean> data = bean.getData();
mpermissionSize = data.size();
// setPermissionListMark();
permissionDetailRvAdapter = new AccountPermissionDetailRvAdapter(R.layout.item_account_permission, data);
permissionDetailRvAdapter.setOnCheckBoxListener(new AccountPermissionDetailRvAdapter.OnSwitchButtonCheckChangeListener() {
@Override
public void onCheckChanged(boolean isChecked,int id) {
//showTip("ischeck:"+isChecked+" ;id:"+id);
if (isChecked){
mStringBuilder.append(id+",");
}else {//根据id来进行判断删除stringbuilder的哪个
// mStringBuilder.delete(id-1,id);
if (String.valueOf(id).length()<2){
int i = mStringBuilder.indexOf("" + id);
Log.d("stringbuilder","index:"+i);
mStringBuilder.deleteCharAt(i);
mStringBuilder.deleteCharAt(i);
}else {
int im = mStringBuilder.indexOf("" + id);
mStringBuilder.deleteCharAt(im);
mStringBuilder.deleteCharAt(im);
mStringBuilder.deleteCharAt(im);
}
}
showTip(mStringBuilder.toString());
}
});
rvMyQuanxian.setLayoutManager(new LinearLayoutManager(this));
rvMyQuanxian.setAdapter(permissionDetailRvAdapter);
}
3.条目布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:id="@+id/tv_permission"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
android:textColor="@color/black191"
android:gravity="center_vertical"
android:layout_marginLeft="24dp"
android:text="点餐"
/>
<CheckBox
android:id="@+id/iv_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center_vertical"
android:layout_marginRight="19dp"
android:layout_gravity="center_vertical"
android:button="@drawable/iv_select_switch_button"/>
</LinearLayout>
小结:主要是stringbuilder的处理,indexof返回首先出现的这个匹配的字符串,如果没有就返回-1,
deleteCharAt删除指定位置的元素