|
|
@@ -0,0 +1,175 @@
|
|
|
+package com.atmob.voiceai.module.integral;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.databinding.ViewDataBinding;
|
|
|
+import androidx.lifecycle.LifecycleOwner;
|
|
|
+import androidx.recyclerview.widget.AsyncListDiffer;
|
|
|
+import androidx.recyclerview.widget.DiffUtil;
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
+
|
|
|
+import com.atmob.voiceai.data.api.bean.GoodsBean;
|
|
|
+import com.atmob.voiceai.databinding.ItemIntegralPurchaseGoodsList1Binding;
|
|
|
+import com.atmob.voiceai.databinding.ItemIntegralPurchaseGoodsList2Binding;
|
|
|
+import com.atmob.voiceai.databinding.ItemIntegralPurchaseGoodsList3Binding;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+public class IntegralPurchaseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
|
+
|
|
|
+
|
|
|
+ private ActionHandler actionHandler;
|
|
|
+ private final AsyncListDiffer<GoodsBean> listDiffer;
|
|
|
+ private final LifecycleOwner lifecycleOwner;
|
|
|
+
|
|
|
+ private GoodsBean checkedGoodsBean;
|
|
|
+
|
|
|
+ public static final int TYPE_FIRST_ROW = 0;
|
|
|
+ public static final int TYPE_SECOND_ROW = 1;
|
|
|
+ public static final int TYPE_THIRD_ROW = 2;
|
|
|
+
|
|
|
+
|
|
|
+ public IntegralPurchaseAdapter(LifecycleOwner lifecycleOwner) {
|
|
|
+ this.lifecycleOwner = lifecycleOwner;
|
|
|
+ listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<GoodsBean>() {
|
|
|
+ @Override
|
|
|
+ public boolean areItemsTheSame(@NonNull GoodsBean oldItem, @NonNull GoodsBean newItem) {
|
|
|
+ return oldItem.getId() == newItem.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean areContentsTheSame(@NonNull GoodsBean oldItem, @NonNull GoodsBean newItem) {
|
|
|
+ return Objects.equals(oldItem.getName(), newItem.getName()) &&
|
|
|
+ oldItem.getAmount() == newItem.getAmount() &&
|
|
|
+ oldItem.getOriginalAmount() == newItem.getOriginalAmount() &&
|
|
|
+ Objects.equals(oldItem.getDescription(), newItem.getDescription()) &&
|
|
|
+ Objects.equals(oldItem.getRemark(), newItem.getRemark()) &&
|
|
|
+ oldItem.isPopular() == newItem.isPopular();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setActionHandler(ActionHandler actionHandler) {
|
|
|
+ this.actionHandler = actionHandler;
|
|
|
+ }
|
|
|
+
|
|
|
+ @NonNull
|
|
|
+ @Override
|
|
|
+ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
+ Context context = parent.getContext();
|
|
|
+ LayoutInflater layoutInflater = LayoutInflater.from(context);
|
|
|
+ if (viewType == TYPE_FIRST_ROW) {
|
|
|
+ return new FirstViewHolder(ItemIntegralPurchaseGoodsList1Binding.inflate(layoutInflater, parent, false));
|
|
|
+ } else if (viewType == TYPE_SECOND_ROW) {
|
|
|
+ return new SecondViewHolder(ItemIntegralPurchaseGoodsList2Binding.inflate(layoutInflater, parent, false));
|
|
|
+ } else {
|
|
|
+ return new ThirdViewHolder(ItemIntegralPurchaseGoodsList3Binding.inflate(layoutInflater, parent, false));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getItemViewType(int position) {
|
|
|
+ return listDiffer.getCurrentList().get(position).getIntegralType();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
|
|
+ if (holder instanceof FirstViewHolder) {
|
|
|
+ ((FirstViewHolder) holder).bind(listDiffer.getCurrentList().get(position));
|
|
|
+ } else if (holder instanceof SecondViewHolder) {
|
|
|
+ ((SecondViewHolder) holder).bind(listDiffer.getCurrentList().get(position));
|
|
|
+ } else {
|
|
|
+ ((ThirdViewHolder) holder).bind(listDiffer.getCurrentList().get(position));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getItemCount() {
|
|
|
+ return listDiffer.getCurrentList().size();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void submitList(List<GoodsBean> list) {
|
|
|
+ if (list != null && !list.isEmpty()) {
|
|
|
+ checkedGoodsBean = list.get(0);
|
|
|
+ checkedGoodsBean.setSelect(true);
|
|
|
+ } else {
|
|
|
+ checkedGoodsBean = null;
|
|
|
+ }
|
|
|
+ if (actionHandler != null) {
|
|
|
+ actionHandler.onItemClick(checkedGoodsBean);
|
|
|
+ }
|
|
|
+ listDiffer.submitList(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCheckedGoodsBean(GoodsBean viewBean) {
|
|
|
+ if (viewBean == checkedGoodsBean) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (checkedGoodsBean != null) {
|
|
|
+ checkedGoodsBean.setSelect(false);
|
|
|
+ }
|
|
|
+ checkedGoodsBean = viewBean;
|
|
|
+ checkedGoodsBean.setSelect(true);
|
|
|
+ if (actionHandler != null) {
|
|
|
+ actionHandler.onItemClick(viewBean);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class FirstViewHolder extends RecyclerView.ViewHolder {
|
|
|
+
|
|
|
+ ItemIntegralPurchaseGoodsList1Binding binding;
|
|
|
+
|
|
|
+ public FirstViewHolder(@NonNull ItemIntegralPurchaseGoodsList1Binding binding) {
|
|
|
+ super(binding.getRoot());
|
|
|
+ this.binding = binding;
|
|
|
+ binding.setLifecycleOwner(lifecycleOwner);
|
|
|
+ binding.setOnItemClick(v -> setCheckedGoodsBean(binding.getGoodsBean()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void bind(GoodsBean goodsBean) {
|
|
|
+ binding.setGoodsBean(goodsBean);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class SecondViewHolder extends RecyclerView.ViewHolder {
|
|
|
+
|
|
|
+ ItemIntegralPurchaseGoodsList2Binding binding;
|
|
|
+
|
|
|
+ public SecondViewHolder(@NonNull ItemIntegralPurchaseGoodsList2Binding binding) {
|
|
|
+ super(binding.getRoot());
|
|
|
+ this.binding = binding;
|
|
|
+ binding.setLifecycleOwner(lifecycleOwner);
|
|
|
+ binding.setOnItemClick(v -> setCheckedGoodsBean(binding.getGoodsBean()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void bind(GoodsBean goodsBean) {
|
|
|
+ binding.setGoodsBean(goodsBean);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class ThirdViewHolder extends RecyclerView.ViewHolder {
|
|
|
+
|
|
|
+ ItemIntegralPurchaseGoodsList3Binding binding;
|
|
|
+
|
|
|
+ public ThirdViewHolder(@NonNull ItemIntegralPurchaseGoodsList3Binding binding) {
|
|
|
+ super(binding.getRoot());
|
|
|
+ this.binding = binding;
|
|
|
+ binding.setLifecycleOwner(lifecycleOwner);
|
|
|
+ binding.setOnItemClick(v -> setCheckedGoodsBean(binding.getGoodsBean()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void bind(GoodsBean goodsBean) {
|
|
|
+ binding.setGoodsBean(goodsBean);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public interface ActionHandler {
|
|
|
+ void onItemClick(GoodsBean goodsBean);
|
|
|
+ }
|
|
|
+}
|