原文链接:https://www.jianshu.com/p/7e1e0da67864
1.布局界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@color/white"
android:orientation="vertical">
<include layout="@layout/action_common_bar" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RelativeLayout
android:id="@+id/layout_edit"
android:layout_width="match_parent"
android:layout_height="@dimen/head_line_height"
android:visibility="gone"
tools:visibility="visible">
<RadioButton
android:id="@+id/rb_select_all"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="全选"
android:textColor="@color/shop_text_selecter"
android:textSize="16sp" />
<Button
android:id="@+id/btn_del"
style="@style/DefaultOrangeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="4dp"
android:text="删除" />
</RelativeLayout>
</LinearLayout>
2.RecyclerView的适配器
public class MyCollectionAdapter extends BaseQuickAdapter<GoodsInfo, DataBindBaseViewHolder> {
private ArrayList<Integer> selectArrays = new ArrayList<>();//选中数据项在列表中的位置集合
private boolean isShowSelectBtn;//是否显示选中按钮,默认不显示
private boolean isSelectAll;//外部【全选】按钮是否为选中状态
public onSelectAllListener listener;//选中和取消选中监听器
public void setListener(onSelectAllListener listener) {
this.listener = listener;
}
public interface onSelectAllListener {
void cancleSelectAll();
void selectAll();
}
public MyCollectionAdapter() {
super(R.layout.item_my_collection);
}
/**
* 选中/取消
*
* @param selectPosition
*/
public void setSelectPosition(Integer selectPosition) {
boolean contains = selectArrays.contains(selectPosition);
if (contains) {
selectArrays.remove(selectPosition);
//取消一项后,如果全选按钮的状态为:选中,应该取消全选按钮选中状态,但不触发取消所有列表已经选中项的事件
if (isSelectAll) {
isSelectAll = false;
if (listener != null) listener.cancleSelectAll();
}
} else {
//如果添加一项后,(外部[全选按钮]为:未选中&&列表数据全部选中),那么设置全选按钮为选中状态,不触发选中事件
selectArrays.add(selectPosition);
if (!isSelectAll && isSelectAllData()) {
isSelectAll = true;
if (listener != null) listener.selectAll();
}
}
Collections.sort(selectArrays);
notifyItemChanged(selectPosition);
}
/**
* 全选
*/
public void selectAll() {
for (int i = 0; i < mData.size(); i++) {
boolean contains = selectArrays.contains(i);
if (!contains) selectArrays.add(i);
}
Collections.sort(selectArrays);//进行排序,避免因为选择前后顺序不一致,导致更新位置越界
L.e("selectArrays==" + selectArrays.toString());
notifyDataSetChanged();
isSelectAll = true;
}
/**
* 取消全选
*/
public void cancleAll() {
selectArrays.clear();
notifyDataSetChanged();
isSelectAll = false;
}
/**
* 是否选中了所有的数据
* 用来设定RadioButton状态
*/
public boolean isSelectAllData() {
return mData.size() == selectArrays.size();
}
/**
* 删除选中项
* position 为选中数据项在列表中的位置,如0,1,2,3
* i 为当前集合遍历位置,所以remove()移除时要使用position-i
* 例如选中 position 1和3,那么遍历两次i为 0,1
* 由于第一次移除位置1的时候,列表数据少了一项,所以原来位置为3的变成了位置2,所以3-1=2
* 也就是position-i才会拿到正确的列表数据位置
*/
public void delSelect() {
for (int i = 0; i < selectArrays.size(); i++) {
Integer position = selectArrays.get(i);
remove(position - i);
}
selectArrays.clear();
}
/**
* 获得所有选中的收藏,并组装为id集合,以“,”分割的字符串
* 方便接口删除收藏项
*
* @return
*/
public String getAllSelectIds() {
if (selectArrays.size() == 0) return "";
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < selectArrays.size(); i++) {
GoodsInfo dataBean = mData.get(selectArrays.get(i));
String goodsId = dataBean.getItemId();
buffer.append(goodsId);
if (i < selectArrays.size() - 1) buffer.append(",");
}
return buffer.toString();
}
/**
* 显示和隐藏操作按钮
*
* @param showSelectBtn
*/
public void setShowSelectBtn(boolean showSelectBtn) {
isShowSelectBtn = showSelectBtn;
notifyDataSetChanged();
}
@Override
protected void convert(DataBindBaseViewHolder helper, GoodsInfo item) {
ItemMyCollectionBinding binding = (ItemMyCollectionBinding) helper.getBinding();
binding.setItem(item);
binding.couponPrice.setCouponText(item.getCouponMoney() + "元");
helper.addOnClickListener(R.id.rb_select);
//是否显示操作按钮
if (isShowSelectBtn) {
binding.rbSelect.setVisibility(View.VISIBLE);
boolean contains = selectArrays.contains(helper.getLayoutPosition());//是否选中
binding.rbSelect.setChecked(contains);
} else {
binding.rbSelect.setVisibility(View.GONE);
}
}
}
其中适配器布局文件item_my_collection
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<import type="cn.com.smallcake_utils.SpannableStringUtils" />
<variable
name="item"
type="**.GoodsInfo" />
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<RadioButton
android:id="@+id/rb_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<ImageView
android:id="@+id/iv_pict_url"
imageRoundUrl="@{item.itemPic}"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/rb_select"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:src="@mipmap/logo" />
<TextView
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:maxLines="2"
android:text="@{item.itemTitle}"
android:textColor="@color/text_black"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/iv_pict_url"
app:layout_constraintTop_toTopOf="parent"
tools:text="婴儿湿巾80抽x5包便携式迷你带盖批发新生儿棉柔巾婴儿湿巾80抽x5包便携式迷你带盖批发新生儿棉柔巾" />
<ImageView
android:id="@+id/iv_user_type"
isTmall="@{item.isTmall}"
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:src="@mipmap/icon_tmall"
app:layout_constraintStart_toEndOf="@+id/iv_pict_url"
app:layout_constraintTop_toBottomOf="@+id/tv_title" />
<TextView
android:id="@+id/tv_shop_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:text="@{item.shopName}"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/iv_user_type"
app:layout_constraintEnd_toStartOf="@+id/tv_volume"
app:layout_constraintStart_toEndOf="@+id/iv_user_type"
app:layout_constraintTop_toTopOf="@+id/iv_user_type"
tools:text="天真贝比旗舰店" />
<TextView
android:id="@+id/tv_volume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text='@{@string/sold+item.itemSale}'
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/tv_shop_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/tv_shop_title"
app:layout_constraintTop_toTopOf="@+id/tv_shop_title"
app:layout_constraintVertical_bias="0.0"
tools:text="@string/sold" />
<TextView
android:id="@+id/tv_commission_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/round2_pink_bg"
android:ellipsize="start"
android:paddingLeft="4dp"
android:paddingTop="2dp"
android:paddingRight="4dp"
android:paddingBottom="2dp"
android:singleLine="true"
android:text='@{@string/ygsy_y+item.commision}'
android:textColor="@color/tab_orange"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="预估收益10元" />
<**.CouponBg
android:id="@+id/coupon_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/tv_commission_price"
app:layout_constraintEnd_toEndOf="parent"
tools:coupon_price="15元" />
<TextView
android:id="@+id/tv_zk_final_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
android:ellipsize="start"
android:singleLine="true"
android:text='@{SpannableStringUtils.getBuilder(@string/quan_hou).setProportion(0.8f).append(@string/rmb_symbol).setProportion(0.7f).setBold().append(item.itemEndPrice).setBold().create()}'
android:textColor="@color/tab_orange"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tv_commission_price"
app:layout_constraintStart_toEndOf="@+id/iv_pict_url"
tools:text="券后¥19.8" />
<TextView
android:id="@+id/tv_reserve_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="4dp"
android:text='@{SpannableStringUtils.getBuilder(@string/yuan_jia+item.itemPrice).setStrikethrough().create()}'
android:textColor="@color/text_gray"
android:textSize="11sp"
app:layout_constraintBottom_toTopOf="@+id/tv_zk_final_price"
app:layout_constraintStart_toEndOf="@+id/iv_pict_url"
tools:text="原价¥149.9" />
<View
android:layout_width="match_parent"
android:layout_height="1.5dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/h_gray_dot_line"
android:layerType="software"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>
3.使用,主要就三个操作+适配器操作
@OnClick({R.id.tv_right, R.id.rb_select_all, R.id.btn_del})
public void doClicks(View view) {
switch (view.getId()) {
case R.id.tv_right://操作的显示和隐藏
edit();
break;
case R.id.rb_select_all://全选/取消全选
clickSelectAll();
break;
case R.id.btn_del://删除
del();
break;
}
}
- a.点击顶部右侧,来显示和隐藏列表中的操作按钮和底部的全选删除按钮
edit()
private void edit() {
String s = tvRight.getText().toString();
if (s.equals("管理")){
tvRight.setText("取消");
mAdapter.setShowSelectBtn(true);
layoutEdit.setVisibility(View.VISIBLE);
}else {
tvRight.setText("管理");
mAdapter.setShowSelectBtn(false);
layoutEdit.setVisibility(View.GONE);
}
}
- b.全选按钮的触发效果
private void clickSelectAll() {
if (mAdapter.isSelectAllData()) {
rbSelectAll.setChecked(false);
mAdapter.cancleAll();
} else {
rbSelectAll.setChecked(true);
mAdapter.selectAll();
}
}
- c.删除
private void del() {
String allSelectIds = mAdapter.getAllSelectIds();
if (TextUtils.isEmpty(allSelectIds)) {
ToastUtil.showLong("未选中任何项!");
return;
}
//刷新适配器
dataProvider.user.delCol(user.getUid(), user.getToken(), allSelectIds)
.subscribe(new OnSuccessAndFailListener<BaseResponse>(dialog) {
@Override
protected void onSuccess(BaseResponse baseResponse) {
ToastUtil.showLong(baseResponse.getMsg());
//刷新适配器
mAdapter.delSelect();
}
});
}
- d.适配器操作
private void onEvent() {
//加载更多
mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
@Override
public void onLoadMoreRequested() {
if (page < totalPages) {
page++;
getMyCollection();
} else {
mAdapter.loadMoreEnd();
}
}
}, recyclerView);
//进入商品详情
mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
GoodsInfo item = (GoodsInfo) adapter.getItem(position);
jumpToFragmentByType(item);
}
});
//单条数据项的选中和取消选中
mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
mAdapter.setSelectPosition(position);
}
});
//全选后,取消一项,解除全选按钮选中状态,选中一项后,如果已经全选,改变全选按钮状态为选中
mAdapter.setListener(new MyCollectionAdapter.onSelectAllListener() {
@Override
public void cancleSelectAll() {
rbSelectAll.setChecked(false);
}
@Override
public void selectAll() {
rbSelectAll.setChecked(true);
}
});
}
PS:适配器使用BaseRecyclerViewAdapterHelper+DataBind的方式,效果如下: