| | |
| | | package com.application.zhangshi_app_android.adapter; |
| | | |
| | | import android.animation.Animator; |
| | | import android.animation.AnimatorListenerAdapter; |
| | | import android.animation.ValueAnimator; |
| | | import android.app.Activity; |
| | | import android.content.Context; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | | import android.view.animation.Animation; |
| | | import android.view.animation.Transformation; |
| | | import android.graphics.Color; |
| | | import android.util.TypedValue; |
| | | import android.widget.FrameLayout; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.databinding.ViewDataBinding; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | import androidx.lifecycle.LifecycleOwner; |
| | | |
| | | import com.android.app_base.base.adapter.BaseRVAdapter; |
| | | import com.android.app_base.base.dialog.BaseDialog; |
| | | import com.android.app_base.base.dialog.UIDialog; |
| | | import com.android.app_base.utils.ScreenSizeUtils; |
| | | import com.android.app_base.utils.Utils; |
| | | import com.android.app_base.utils.rxbus.MessageEvent; |
| | | import com.android.app_base.utils.rxbus.RxBus; |
| | | import com.application.zhangshi_app_android.R; |
| | | import com.application.zhangshi_app_android.bean.ConsumeRecordBean; |
| | | import com.application.zhangshi_app_android.bean.DailyConsumeRecordBean; |
| | | import com.application.zhangshi_app_android.databinding.ItemDailyConsumeRecordBinding; |
| | | import com.application.zhangshi_app_android.databinding.ItemPropertyBinding; |
| | | import com.application.zhangshi_app_android.ui.function.TourismDetailActivityViewModel; |
| | | import com.github.gzuliyujiang.wheelpicker.DatePicker; |
| | | import com.github.gzuliyujiang.wheelpicker.contract.OnDatePickedListener; |
| | | import com.github.gzuliyujiang.wheelpicker.entity.DateEntity; |
| | | import com.github.gzuliyujiang.wheelpicker.impl.UnitDateFormatter; |
| | | import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.Calendar; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | import io.reactivex.functions.Consumer; |
| | | |
| | | /** |
| | | * @author Ljj |
| | |
| | | * @desc 每日消费记录适配器 |
| | | */ |
| | | public class DailyConsumeRecordRvAdapter extends BaseRVAdapter<DailyConsumeRecordBean, ItemDailyConsumeRecordBinding, DailyConsumeRecordRvAdapter.ViewHolder> { |
| | | public DailyConsumeRecordRvAdapter(Context context) { |
| | | private TourismDetailActivityViewModel viewModel; |
| | | |
| | | private Map<DailyConsumeRecordBean, Boolean> expendMap; |
| | | |
| | | public DailyConsumeRecordRvAdapter(Context context, TourismDetailActivityViewModel viewModel) { |
| | | super(context); |
| | | this.viewModel = viewModel; |
| | | this.expendMap = new HashMap<>(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | protected void onBind(ViewHolder holder, int position) { |
| | | holder.getBinding().setBean(getItem(position)); |
| | | holder.getBinding().setIsFold(holder.isExpended); |
| | | ConsumeRecordRvAdapter adapter = new ConsumeRecordRvAdapter(mContext); |
| | | holder.getBinding().rvConsumeRecord.setAdapter(adapter); |
| | | adapter.setData(getItem(position).getConsumeRecordBeans()); |
| | | holder.getBinding().ivFold.setOnClickListener(v -> { |
| | | if (holder.isExpended) { |
| | | holder.isExpended = false; |
| | | holder.getBinding().setIsFold(false); |
| | | collapseView(holder.getBinding().rvConsumeRecord, holder.getBinding().rvConsumeRecord); |
| | | } else { |
| | | holder.isExpended = true; |
| | | holder.getBinding().setIsFold(true); |
| | | expandView(holder.getBinding().rvConsumeRecord); |
| | | DailyConsumeRecordBean item = getItem(position); |
| | | holder.getBinding().setLifecycleOwner((LifecycleOwner) mContext); |
| | | holder.getBinding().setBean(item); |
| | | holder.getBinding().setViewModel(viewModel); |
| | | if (!expendMap.containsKey(getItem(position))){ |
| | | expendMap.put(getItem(position),false); |
| | | } |
| | | //根据主题选择对应图标 |
| | | TypedValue unfoldIcon = new TypedValue(); |
| | | mContext.getTheme().resolveAttribute(R.attr.icUnfold, unfoldIcon, true); |
| | | TypedValue foldIcon = new TypedValue(); |
| | | mContext.getTheme().resolveAttribute(R.attr.icFold, foldIcon, true); |
| | | if (Boolean.TRUE.equals(expendMap.get(getItem(position)))){ |
| | | holder.getBinding().ivFold.setBackgroundResource(unfoldIcon.resourceId); |
| | | holder.getBinding().cardView.post(() -> { |
| | | holder.getBinding().cardView.getLayoutParams().height = FrameLayout.LayoutParams.WRAP_CONTENT; |
| | | holder.getBinding().cardView.requestLayout(); |
| | | }); |
| | | }else{ |
| | | holder.getBinding().ivFold.setBackgroundResource(foldIcon.resourceId); |
| | | holder.getBinding().cardView.post(() -> { |
| | | holder.getBinding().cardView.getLayoutParams().height = holder.getBinding().layoutTitle.getMeasuredHeight(); |
| | | holder.getBinding().cardView.requestLayout(); |
| | | }); |
| | | } |
| | | holder.getBinding().layoutTitle.setOnClickListener(v -> { |
| | | if (Boolean.TRUE.equals(expendMap.get(getItem(position)))){ |
| | | int initialHeight = holder.getBinding().cardView.getMeasuredHeight(); |
| | | int collapsedHeight = holder.getBinding().layoutTitle.getMeasuredHeight(); |
| | | Utils.pullCollapse(holder.getBinding().cardView,initialHeight,collapsedHeight); |
| | | holder.getBinding().ivFold.setBackgroundResource(foldIcon.resourceId); |
| | | expendMap.put(getItem(position),false); |
| | | }else{ |
| | | final int initialHeight = holder.getBinding().cardView.getMeasuredHeight(); |
| | | holder.getBinding().cardView.measure(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT); |
| | | int targetHeight = holder.getBinding().cardView.getMeasuredHeight(); |
| | | Utils.dropExpand(holder.getBinding().cardView,initialHeight,targetHeight); |
| | | holder.getBinding().ivFold.setBackgroundResource(unfoldIcon.resourceId); |
| | | expendMap.put(getItem(position),true); |
| | | holder.getBinding().cardView.postDelayed(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | holder.getBinding().cardView.getLayoutParams().height = FrameLayout.LayoutParams.WRAP_CONTENT; |
| | | holder.getBinding().cardView.setLayoutParams(holder.getBinding().cardView.getLayoutParams()); |
| | | } |
| | | },500); |
| | | |
| | | } |
| | | }); |
| | | |
| | | |
| | | ConsumeRecordRvAdapter adapter = new ConsumeRecordRvAdapter(mContext,viewModel); |
| | | holder.getBinding().rvConsumeRecord.setAdapter(adapter); |
| | | adapter.setData(getItem(position).getConsumeRecordBeans()); |
| | | |
| | | holder.getBinding().ivAdd.setOnClickListener(v -> { |
| | | ConsumeRecordBean bean = new ConsumeRecordBean(); |
| | | adapter.addItem(0,bean); |
| | | }); |
| | | holder.getBinding().ivDelete.setOnClickListener(v -> { |
| | | new UIDialog.Builder(mContext) |
| | | .setTitle("您确定要删除该日消费记录吗") |
| | | .setContent("删除后将无法恢复") |
| | | .setConfirm("确定") |
| | | .setCancel("取消") |
| | | .setListener(new UIDialog.OnListener() { |
| | | @Override |
| | | public void onConfirm(BaseDialog dialog) { |
| | | removeItem(position); |
| | | RxBus.getInstance().post(new MessageEvent(MessageEvent.EVENT_TOURISM_CONSUME_CHANGE)); |
| | | } |
| | | }) |
| | | .show(); |
| | | }); |
| | | |
| | | holder.getBinding().tvDate.setOnClickListener(v -> { |
| | | DatePicker datePicker = getDatePicker(); |
| | | datePicker.setOnDatePickedListener(new OnDatePickedListener() { |
| | | @Override |
| | | public void onDatePicked(int year, int month, int day) { |
| | | holder.getBinding().tvDate.setText(month+"月"+day+"日"); |
| | | } |
| | | }); |
| | | datePicker.show(); |
| | | }); |
| | | RxBus.getInstance().toObservable((LifecycleOwner) mContext, MessageEvent.class) |
| | | .subscribe(messageEvent -> { |
| | | if (messageEvent.getMessageEventCode() == MessageEvent.EVENT_TOURISM_CONSUME_CHANGE){ |
| | | if (adapter.getItemCount() == 0 || adapter.getData() == null){ |
| | | holder.getBinding().etDailyMoney.setText("0.0"); |
| | | return; |
| | | } |
| | | BigDecimal bigDecimal = new BigDecimal(0); |
| | | for (ConsumeRecordBean bean : adapter.getData()){ |
| | | bigDecimal = bigDecimal.add(BigDecimal.valueOf(bean.getTotalCost())); |
| | | } |
| | | holder.getBinding().etDailyMoney.setText(bigDecimal.stripTrailingZeros().toPlainString()); |
| | | for (ConsumeRecordBean bean : adapter.getData()){ |
| | | bean.setDayCost(item.getDailyMoney()); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return new ViewHolder(itemBind); |
| | | } |
| | | |
| | | |
| | | public static class ViewHolder extends BaseRVAdapter.BaseViewHolder<ItemDailyConsumeRecordBinding> { |
| | | private boolean isExpended; |
| | | |
| | | public ViewHolder(ItemDailyConsumeRecordBinding binding) { |
| | | super(binding); |
| | | } |
| | | } |
| | | |
| | | public void collapseView(View initialView, View collapsedView){ |
| | | int initialHeight = initialView.getMeasuredHeight(); |
| | | int collapsedHeight = collapsedView.getMeasuredHeight(); |
| | | int distanceToCollapse = (int) (initialHeight - collapsedHeight); |
| | | |
| | | Animation a = new Animation() { |
| | | @Override |
| | | protected void applyTransformation(float interpolatedTime, Transformation t) { |
| | | if (interpolatedTime == 1){ |
| | | } |
| | | initialView.getLayoutParams().height = (int) (initialHeight - (distanceToCollapse * interpolatedTime)); |
| | | initialView.requestLayout(); |
| | | } |
| | | |
| | | @Override |
| | | public boolean willChangeBounds() { |
| | | return true; |
| | | } |
| | | }; |
| | | a.setDuration(500); |
| | | initialView.startAnimation(a); |
| | | public void setFold(boolean isFold,int position){ |
| | | expendMap.put(getItem(position),isFold); |
| | | notifyItemChanged(position); |
| | | } |
| | | public void expandView(View initialView){ |
| | | int initialHeight = initialView.getMeasuredHeight(); |
| | | initialView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); |
| | | int targetHeight = initialView.getMeasuredHeight(); |
| | | ValueAnimator animator = ValueAnimator.ofInt(initialHeight,targetHeight); |
| | | animator.addUpdateListener(animation -> { |
| | | initialView.getLayoutParams().height = (int) animation.getAnimatedValue(); |
| | | initialView.requestLayout(); |
| | | }); |
| | | animator.addListener(new AnimatorListenerAdapter() { |
| | | @Override |
| | | public void onAnimationEnd(Animator animation) { |
| | | initialView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; |
| | | initialView.setLayoutParams(initialView.getLayoutParams()); |
| | | } |
| | | }); |
| | | animator.setDuration(500); |
| | | animator.start(); |
| | | |
| | | private DatePicker getDatePicker(){ |
| | | com.github.gzuliyujiang.wheelpicker.DatePicker datePicker = new DatePicker((Activity) mContext); |
| | | DateWheelLayout wheelLayout = datePicker.getWheelLayout(); |
| | | wheelLayout.getMonthWheelView().setCurtainCorner(0);//背景无圆角 |
| | | wheelLayout.getDayWheelView().setCurtainCorner(5);//背景右边有圆角 |
| | | wheelLayout.getYearWheelView().setCurtainCorner(4);//背景左边有圆角 |
| | | wheelLayout.setDateFormatter(new UnitDateFormatter()); |
| | | wheelLayout.setPadding(50, 0, 50, 0); |
| | | wheelLayout.setItemSpace(ScreenSizeUtils.dip2px(mContext, 20)); |
| | | wheelLayout.setCurtainEnabled(true); |
| | | wheelLayout.setCurtainColor(Color.parseColor("#4D2F63F8")); |
| | | wheelLayout.setCurtainRadius(ScreenSizeUtils.dip2px(mContext,4)); |
| | | wheelLayout.setSelectedTextColor(Color.parseColor("#FF2F63F8")); |
| | | wheelLayout.setTextColor(Color.parseColor("#FF999999")); |
| | | wheelLayout.setIndicatorEnabled(false); |
| | | //日期可选范围 |
| | | wheelLayout.setRange(DateEntity.target(1900, 1, 1), DateEntity.target(2500, 12, 31),DateEntity.target(Calendar.getInstance())); |
| | | datePicker.getWheelLayout().setResetWhenLinkage(false); |
| | | return datePicker; |
| | | } |
| | | } |