黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    /// <summary>
    /// 做成一个上下可以滑动的列表控件,有刷新功能(它会调整高度,无桌布)
    /// </summary>
    public class VerticalListRefreshControl : VerticalRefreshLayout
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 行之间的间距
        /// </summary>
        public int rowSpace = 0;
        /// <summary>
        /// 最大高度
        /// </summary>
        private int maxHeight = -1;
        /// <summary>
        /// 一个没什么用的东西
        /// </summary>
        private FrameLayout frameBackTemp = null;
        /// <summary>
        /// 自定义的获取子控件个数
        /// </summary>
        public new int ChildrenCount
        {
            get
            {
                int count = base.ChildrenCount;
                if (frameBackTemp != null && frameBackTemp.Parent != null)
                {
                    count--;
                }
                return count < 0 ? 0 : count;
            }
        }
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 做成一个上下可以滑动的列表控件,有刷新功能(它会调整高度,无桌布)
        /// </summary>
        /// <param name="i_rowSpace">行之间的间距(这个值是与行控件绑定一起使用的)</param>
        public VerticalListRefreshControl(int i_rowSpace = 0)
        {
            this.rowSpace = Application.GetRealHeight(i_rowSpace);
            this.VerticalScrollBarEnabled = false;
#if iOS
            //自动偏移取消
            if (UIKit.UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                (this.uiView as UIKit.UIScrollView).ContentInsetAdjustmentBehavior = UIKit.UIScrollViewContentInsetAdjustmentBehavior.Never;
            }
#endif
        }
 
        #endregion
 
        #region ■ 添加子控件_________________________
 
        /// <summary>
        /// 添加子控件(FrameRowControl,RowLayoutControl会改变高度)
        /// </summary>
        /// <param name="view"></param>
        public override void AddChidren(View view)
        {
            base.AddChidren(view);
            if (view is FrameRowControl || view is RowLayoutControl)
            {
                if (rowSpace > 0)
                {
                    view.Height += rowSpace;
                }
            }
            if (maxHeight == -1)
            {
                maxHeight = this.Height;
            }
        }
        #endregion
 
        #region ■ 调整真实高度_______________________
 
        /// <summary>
        /// 还原高度
        /// </summary>
        public void RecoverHeight()
        {
            if (this.maxHeight != -1)
            {
                this.Height = this.maxHeight;
#if iOS
                this.ReLocation();
#endif
            }
        }
 
        /// <summary>
        /// 调整控件真实高度(只针对行控件都是相同高度的,高度只会减少,不会增加)
        /// </summary>
        /// <param name="bottomSpace">底部空白间距(真实值)</param>
        /// <param name="addSpace">当真实高度超过原有高度时,是否添加空白</param>
        public void AdjustRealHeight(int bottomSpace, bool addSpace = true)
        {
            int count = this.ChildrenCount;
            if (count <= 0)
            {
                frameBackTemp?.RemoveFromParent();
                frameBackTemp = null;
                if (maxHeight != -1)
                {
                    //还原为最大高度
                    this.Height = maxHeight;
#if iOS
                    this.ReLocation();
#endif
                }
                return;
            }
 
            //调整列表控件的高度
            var realHeight = count * this.GetChildren(0).Height + bottomSpace;
            if (realHeight < this.Height)
            {
                frameBackTemp?.RemoveFromParent();
                frameBackTemp = null;
                //缩小控件高度
                this.Height = realHeight;
#if iOS
                this.ReLocation();
#endif
            }
            else if (addSpace == true && bottomSpace > 0 && realHeight > this.maxHeight)
            {
                frameBackTemp?.RemoveFromParent();
 
                frameBackTemp = new FrameLayout();
                frameBackTemp.Height = bottomSpace;
                this.AddChidren(frameBackTemp);
            }
        }
 
        /// <summary>
        /// 针对底部点击按钮,调整控件真实高度
        /// </summary>
        /// <param name="bottomSpace">底部空白间距(真实值,如果列表控件真实高度没有超过时,使用此值)</param>
        /// <param name="correctionsValue">Y轴补正值(真实值,列表控件不在bodyFramelayout的时候使用)</param>
        public void AdjustRealHeightByBottomButton(int bottomSpace, int correctionsValue = 0)
        {
            if (this.ChildrenCount == 0)
            {
                frameBackTemp?.RemoveFromParent();
                frameBackTemp = null;
                if (maxHeight != -1)
                {
                    //还原为最大高度
                    this.Height = maxHeight;
#if iOS
                    this.ReLocation();
#endif
                }
                return;
            }
            var realHeight = this.ChildrenCount * this.GetChildren(0).Height + this.Y + correctionsValue;
            var btnTemp = new BottomClickButton();
            if (btnTemp.Yaxis >= realHeight)
            {
                //没有超过
                this.AdjustRealHeight(bottomSpace);
                return;
            }
            //超过时,重置至最大
            this.RecoverHeight();
 
            //添加临时控件,直至可以滑动超过底部按钮
            frameBackTemp?.RemoveFromParent();
            frameBackTemp = new FrameLayout();
            frameBackTemp.Height = HdlControlResourse.BodyFrameHeight - btnTemp.Yaxis + bottomSpace;
            this.AddChidren(frameBackTemp);
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 控件移除
        /// </summary>
        public override void RemoveFromParent()
        {
            if (this.Parent != null)
            {
                base.RemoveFromParent();
            }
        }
 
        /// <summary>
        /// ☆☆移除全部控件☆☆
        /// </summary>
        public override void RemoveAll()
        {
            if (this.Parent != null)
            {
                base.RemoveAll();
            }
        }
 
        #endregion
    }
}