Linjiajia
2023-07-25 82e57df230ecb744af6c8865f80870ba03c86d89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.application.zhangshi_app_android.ui.function;
 
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
 
import com.application.zhangshi_app_android.BR;
import com.application.zhangshi_app_android.R;
import com.application.zhangshi_app_android.adapter.OldSpouseRvAdapter;
import com.application.zhangshi_app_android.databinding.ActivityMarriageBinding;
import com.application.zhangshi_app_android.ui.DLBaseActivity;
 
/**
 * @author Ljj
 * @date 2023.04.23. 23:23
 * @desc 婚姻状况 Activity
 */
public class MarriageActivity extends DLBaseActivity<ActivityMarriageBinding,MarriageActivityViewModel> {
 
    private OldSpouseRvAdapter adapter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_marriage;
    }
 
    @Override
    public int getVariableId() {
        return BR.viewModel;
    }
 
    @Override
    public void initParam() {
 
    }
    @Override
    public void initView() {
        adapter = new OldSpouseRvAdapter(this);
        binding.rvPredecessor.setNestedScrollingEnabled(false);
        binding.rvPredecessor.setAdapter(adapter);
 
        final int SCROLL_THRESHOLD = 400; // 滑动阈值,按钮在滑动超过该阈值后开始根据超出阈值距离渐渐显示出来
        final int SCROLL_DISTANCE = 400; // 按钮从隐藏到全部显示出来需要滑动的距离
        binding.fabMoveToTop.setAlpha(0f);
        binding.nestedScrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                // 当滑动距离超过阈值一半时,开始慢慢显示按钮
                if (scrollY >= SCROLL_THRESHOLD) {
                    // 根据滑动距离设置按钮透明度
                    float alpha = Math.min(1.0f, (float) (scrollY - SCROLL_THRESHOLD) / SCROLL_DISTANCE);
                    binding.fabMoveToTop.setAlpha(alpha);
                } else {
                    binding.fabMoveToTop.setAlpha(0f);
                }
            }
        });
        binding.fabMoveToTop.setOnClickListener(v -> {
            // 点击按钮返回顶部
            binding.nestedScrollView.smoothScrollTo(0, 0);
        });
    }
 
    @Override
    public void initData() {
        viewModel.getMarriageInfo();
    }
 
    @Override
    public void initLiveDataObserve() {
        viewModel.getIsSpouseExpendedLiveData().observe(this, aBoolean -> {
            if (aBoolean){
                binding.layoutTitleSpouse.setOnClickListener(view -> {
                    collapseView(binding.cardSpouse,binding.layoutTitleSpouse);
                    viewModel.getIsSpouseExpendedLiveData().setValue(false);
                });
            }else {
                binding.layoutTitleSpouse.setOnClickListener(view -> {
                    expendView(binding.cardSpouse);
                    viewModel.getIsSpouseExpendedLiveData().setValue(true);
                });
            }
        });
        viewModel.getIsPredecessorExpendedLiveData().observe(this, aBoolean -> {
            if (aBoolean){
                binding.layoutTitlePredecessor.setOnClickListener(view -> {
                    collapseView(binding.cardPredecessor,binding.layoutTitlePredecessor);
                    viewModel.getIsPredecessorExpendedLiveData().setValue(false);
                });
            }else {
                binding.layoutTitlePredecessor.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        expendView(binding.cardPredecessor);
                        viewModel.getIsPredecessorExpendedLiveData().setValue(true);
                    }
                });
            }
        });
        viewModel.getIsProcreateExpendedLiveData().observe(this, aBoolean -> {
            if (aBoolean){
                binding.layoutTitleProcreate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        collapseView(binding.cardProcreate,binding.layoutTitleProcreate);
                        viewModel.getIsProcreateExpendedLiveData().setValue(false);
                    }
                });
            }else {
                binding.layoutTitleProcreate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        expendView(binding.cardProcreate);
                        viewModel.getIsProcreateExpendedLiveData().setValue(true);
                    }
                });
            }
        });
        viewModel.getInfoLiveData().observe(this, marriageInfoBean -> {
            if (marriageInfoBean != null){
                if (marriageInfoBean.getOldSpouseList() == null || marriageInfoBean.getOldSpouseList().size() == 0) {
                    binding.cardSpouse.setVisibility(View.GONE);
                }else {
                    binding.cardSpouse.setVisibility(View.VISIBLE);
                    adapter.setData(marriageInfoBean.getOldSpouseList());
                }
            }
        });
    }
 
    @Override
    protected void dataNull() {
        super.dataNull();
        binding.layoutDataNull.setVisibility(View.VISIBLE);
        binding.nestedScrollView.setVisibility(View.GONE);
    }
 
    @Override
    protected void hide() {
        super.hide();
        binding.layoutDataNull.setVisibility(View.GONE);
        binding.nestedScrollView.setVisibility(View.VISIBLE);
    }
 
    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 expendView(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();
    }
 
}