在RecyclerView中实现多选单选功能
程序员文章站
2022-05-31 11:50:13
...
老规则,先上效果图:
实现代码如下:
public class SummaryOneTransAdapter extends RecyclerView.Adapter<SummaryOneTransAdapter.ViewHolder> {
private List<SummaryOneTransactionElement> list;
public SummaryOneTransAdapter(List<SummaryOneTransactionElement> list) {
this.list = list;
}
private IOnItemListener listener;
private Map<Integer, ViewHolder> map = new HashMap<>();
public IOnItemListener getListener() {
return listener;
}
public void setListener(IOnItemListener listener) {
this.listener = listener;
}
public interface IOnItemListener {
void onItemClick(View view, int position);
}
/**
* 显示一类交易统计的要素
*/
public static class SummaryOneTransactionElement {
/**
* 图片资源id
*/
private int iconId;
/**
* 交易名称
*/
private String transactionName;
/**
* 交易名称
*/
private String totalAmount;
/**
* 交易金额
*/
private String saleAmount;
/**
* 交易金额
*/
private String saleNum;
/**
* 退款金额
*/
private String voidAmount;
/**
* 退款笔数
*/
private String voidNum;
public int getIconId() {
return iconId;
}
public void setIconId(int iconId) {
this.iconId = iconId;
}
public String getTransactionName() {
return transactionName;
}
public void setTransactionName(String transactionName) {
this.transactionName = transactionName;
}
public String getSaleAmount() {
return saleAmount;
}
public void setSaleAmount(String saleAmount) {
this.saleAmount = saleAmount;
}
public String getSaleNum() {
return saleNum;
}
public void setSaleNum(String saleNum) {
this.saleNum = saleNum;
}
public String getVoidAmount() {
return voidAmount;
}
public void setVoidAmount(String voidAmount) {
this.voidAmount = voidAmount;
}
public String getVoidNum() {
return voidNum;
}
public void setVoidNum(String voidNum) {
this.voidNum = voidNum;
}
public String getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(String totalAmount) {
this.totalAmount = totalAmount;
}
public SummaryOneTransactionElement() {
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_trans_summary, viewGroup, false);
final ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
SummaryOneTransactionElement element = getList().get(i);
viewHolder.ivTransName.setImageResource(element.getIconId());
if (!TextUtils.isEmpty(element.getTransactionName())) {
viewHolder.tvTransName.setText(element.getTransactionName());
}
if (!TextUtils.isEmpty(element.getTotalAmount())) {
viewHolder.tvTotalAmount.setText(element.getTotalAmount());
} else {
viewHolder.tvVoidAmount.setText("¥0.00");
}
if (!TextUtils.isEmpty(element.getSaleAmount())) {
viewHolder.tvSaleAmount.setText(element.getSaleAmount());
} else {
viewHolder.tvSaleAmount.setText("¥0.00");
}
if (!TextUtils.isEmpty(element.getSaleNum())) {
viewHolder.tvSaleNum.setText(element.getSaleNum());
} else {
viewHolder.tvSaleNum.setText("0");
}
if (!TextUtils.isEmpty(element.getVoidAmount())) {
viewHolder.tvVoidAmount.setText(element.getVoidAmount());
} else {
viewHolder.tvVoidAmount.setText("¥0.00");
}
if (!TextUtils.isEmpty(element.getVoidNum())) {
viewHolder.tvVoidNum.setText(element.getVoidNum());
} else {
viewHolder.tvVoidNum.setText("0");
}
viewHolder.llSummary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!StringUtils.isNull(listener)) {
listener.onItemClick(view, i);
}
}
});
map.put(i, viewHolder);
}
@Override
public int getItemCount() {
if (getList() != null) {
return getList().size();
}
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout llSummary;
/**
* 交易图标
*/
private ImageView ivTransName;
/**
* 交易名称
*/
private TextView tvTransName;
/**
* 金额
*/
private TextView tvTotalAmount;
/**
* 收款金额
*/
private TextView tvSaleAmount;
/**
* 收款笔数
*/
private TextView tvSaleNum;
/**
* 退款金额
*/
private TextView tvVoidAmount;
/**
* 退款笔数
*/
private TextView tvVoidNum;
public ViewHolder(View itemView) {
super(itemView);
llSummary = itemView.findViewById(R.id.ll_summary);
ivTransName = itemView.findViewById(R.id.iv_trans_name);
tvTransName = itemView.findViewById(R.id.tv_trans_name);
tvTotalAmount = itemView.findViewById(R.id.tv_total_amount);
tvSaleAmount = itemView.findViewById(R.id.tv_sale_amount);
tvSaleNum = itemView.findViewById(R.id.tv_sale_num);
tvVoidAmount = itemView.findViewById(R.id.tv_void_amount);
tvVoidNum = itemView.findViewById(R.id.tv_void_num);
}
}
public List<SummaryOneTransactionElement> getList() {
return list;
}
/**
* 刷新区局按钮点击状态
*/
public void refreshUI(int position, boolean select) {
for (Map.Entry<Integer, ViewHolder> entry : map.entrySet()) {
ViewHolder holder = entry.getValue();
// 点击项背景以及字体变色,其它项背景及字体设置为默认颜色
if (entry.getKey() == position) {
if (select) {
holder.llSummary.setBackgroundColor(Color.rgb(0x00, 0x00, 0x00));
} else {
holder.llSummary.setBackgroundColor(Color.rgb(0xff, 0xff, 0xff));
}
}
}
}
RecyclerView recyclerView = findViewById(R.id.rv_summary_list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<SummaryOneTransAdapter.SummaryOneTransactionElement> list = new ArrayList<>();
initSummaryList(list);
final SummaryOneTransAdapter adapter = new SummaryOneTransAdapter(list);
recyclerView.setAdapter(adapter);
final Map<Integer, Boolean> selectMap = new HashMap<>();
adapter.setListener(new SummaryOneTransAdapter.IOnItemListener() {
@Override
public void onItemClick(View view, int position) {
if (selectMap.get(position) != null && selectMap.get(position) == true) {
selectMap.remove(position);
adapter.refreshUI(position, false);
} else {
selectMap.put(position, true);
adapter.refreshUI(position, true);
}
for (Map.Entry<Integer, Boolean> entry : selectMap.entrySet()) {
//获取选中的索引
int key = entry.getKey();
}
}
});
下一篇: ImageView控件
推荐阅读
-
iOS中在APP内加入AppStore评分功能的实现方法
-
Android中RecyclerView拖拽、侧删功能的实现代码
-
Android中RecyclerView实现滑动删除与拖拽功能
-
Android ListView实现单选及多选等功能示例
-
Android ListView实现单选及多选等功能示例
-
android在连拍菜单中增加连拍张数选项功能实现代码
-
Android Recyclerview实现多选,单选,全选,反选,批量删除的功能
-
Android一步步带你在RecyclerView上面实现"拖放"和"滑动删除"功能
-
Android使用AlertDialog实现的信息列表单选、多选对话框功能
-
vue v-model实现自定义样式多选与单选功能