1
wxr
2023-03-31 7e42cc13a14b7de31c9f5d5c61cdf24f3246335d
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
using System;
using Android.Widget;
using Android.Content;
using Android.Views;
using Android.Util;
using Android.Runtime;
using Android.Views.Animations;
using Android.Graphics.Drawables;
 
namespace Shared
{
 
    // 存在问题,下拉后不松开手改为上拉,会拦截MotionEventActions.Up事件,导致触发不了刷新
 
    //VerticalRefreshScrolViewLayout 带下拉刷新
    /// <summary>
    /// 位置布局
    /// </summary>
    public class VerticalRefreshScrolViewLayout : ViewGroup
    {
        public Action BeginHeaderRefreshingAction;
        //public Action beginFooterRefreshingAction;
        //private LinearLayout mainLayout;
 
        private MyLoadingHorizontal mHeaderLoadingView;//下拉刷新头部View
        //private MyLoadingHorizontal mFooterLoadingView;//上拉加载底部View
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public VerticalRefreshScrolViewLayout()
        {
            viewGroup = new AndroidRefreshScrolView(Application.Activity, null);
            (viewGroup as AndroidRefreshScrolView).AlreadyToTopAction += () =>
            {
                 ShowHeaderLoadingView();
            };
 
            (viewGroup as AndroidRefreshScrolView).AlreadyToTopUpAction += () =>
            {
                ShowHeaderAnimationBeginToRefresh();
            };
 
            mHeaderLoadingView = new MyLoadingHorizontal(Application.Activity);
            HideHeaderLoadingView();
            realViewGroup = new AndroidLinearLayout(Application.Activity, this);
            (realViewGroup as AndroidLinearLayout).Orientation = Orientation.Vertical;
            viewGroup.AddView(realViewGroup);
            realViewGroup.AddView(mHeaderLoadingView);
 
            _isCanHeaderRefresh = true;
 
        }
 
 
   
 
 
        /// <summary>
        /// 视图宽度
        /// </summary>
        /// <value>The width.</value>
        public override int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                var layoutParameters = realViewGroup.LayoutParameters;
                layoutParameters.Width = Width;
                realViewGroup.LayoutParameters = layoutParameters;

            }
        }

        /// <summary>
        /// 视图高度
        /// </summary>
        /// <value>The height.</value>
        public override int Height
        {
            get
            {
                return base.Height;
            }
            set
            {
                base.Height = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                var layoutParameters = realViewGroup.LayoutParameters;
                layoutParameters.Height = Height;
                realViewGroup.LayoutParameters = layoutParameters;
            }
        }

        /// <summary>
        /// 是否允许滑动
        /// </summary>
        /// <value><c>true</c> if scroll enabled; otherwise, <c>false</c>.</value>
        public bool ScrollEnabled
        {
            get
            {
                return (viewGroup as AndroidScrolView).ScrollEnable;
            }
            set
            {
                (viewGroup as AndroidScrolView).ScrollEnable = value;
            }
        }

        /// <summary>
        /// 是否正在滑动
        /// </summary>
        /// <value><c>true</c> if decelerating; otherwise, <c>false</c>.</value>
        public bool Decelerating
        {
            get
            {
                return false;
            }
        }
 
        //***************************下拉刷新***************************
        private void ShowHeaderLoadingView()
        {
            if (!IsCanHeaderRefresh) return;
 
            if (IsHeaderRefreshing) return;
 
            mHeaderLoadingView.HideProgress();
            SetHeaderLoadingViewText(_mHeaderReadyRefreshText);
 
            mHeaderLoadingView.Visibility = ViewStates.Visible;
 
      
            //(viewGroup as AndroidRefreshScrolView).showHeader();
            //showHeader();
 
        }
 
        private void ShowHeaderAnimationBeginToRefresh()
        {
 
            if (!IsCanHeaderRefresh) return;
 
 
            if (!IsHeaderRefreshing)
            {
                mHeaderLoadingView.ShowProgress();
                SetHeaderLoadingViewText(_mHeaderRefreshingText);
 
                if (mHeaderLoadingView.Animation == null)
                {
                    //缩放动画
                    mHeaderLoadingView.Animation = new ScaleAnimation(
                        1.0f, 1.2f, 1.0f, 1.2f,
                        Dimension.RelativeToSelf, 0.5f,
                        Dimension.RelativeToSelf, 0.5f);
                    mHeaderLoadingView.Animation.Duration = 300;
 
                }
 
                mHeaderLoadingView.Animation.Reset();
                mHeaderLoadingView.Animation.StartNow();
                //开始刷新
                IsHeaderRefreshing = true;
                BeginHeaderRefreshingAction?.Invoke();
            }
        }
 
        private void SetHeaderLoadingViewText(string mes)
        {
            mHeaderLoadingView.textView.Text = mes;
        }
 
 
        private void HideHeaderLoadingView()
        {
 
            mHeaderLoadingView.Visibility = ViewStates.Gone;
 
            //(viewGroup as AndroidRefreshScrolView).hideHeader();
            //hideHeader();
 
 
        }
 
 
 
        string _mHeaderRefreshingText = "正在刷新...";
        public string MHeaderRefreshingText
        {
            get
            {
                return _mHeaderRefreshingText;
            }
            set
            {
                _mHeaderRefreshingText = value;
            }
        }
 
        string _mHeaderReadyRefreshText = "松开立即刷新";
        public string MHeaderReadyRefreshText
        {
            get
            {
                return _mHeaderReadyRefreshText;
            }
            set
            {
                _mHeaderReadyRefreshText = value;
            }
        }
 
 
 
 
        /// <summary>
        /// 是否支持下拉刷新
        /// </summary>
        bool _isCanHeaderRefresh;
        public bool IsCanHeaderRefresh
        {
            get
            {
                return _isCanHeaderRefresh;
            }
            set
            {
                _isCanHeaderRefresh = value;
            }
        }
 
        /// <summary>
        /// 是否正在下拉刷新
        /// </summary>
        bool _isHeaderRefreshing;
        public bool IsHeaderRefreshing {
            get
            {
                return _isHeaderRefreshing;
            }
            set
            {
                _isHeaderRefreshing = value;
            }
        }
 
 
        ///// <summary>
        ///// 启用或者关闭下拉刷新
        ///// </summary>
        //public void openHeaderRefresh(bool bOpen)
        //{
 
        //}
 
        /// <summary>
        /// 头开始刷新
        /// </summary>
        public void BeginHeaderRefreshing()
        {
            if (!IsCanHeaderRefresh) return;
 
 
            if (!IsHeaderRefreshing)
            {
 
                ShowHeaderLoadingView();
 
                //mHeaderLoadingView.Visibility = ViewStates.Visible;
                ShowHeaderAnimationBeginToRefresh();
 
            }
        }
 
        /// <summary>
        /// 头结束刷新
        /// </summary>
        public void EndHeaderRefreshing()
        {
            if (!IsCanHeaderRefresh) return;
 
            if (IsHeaderRefreshing)
            {
                HideHeaderLoadingView();
                IsHeaderRefreshing = false;
            }
        }
 
 
        //public void showHeader()
        //{
 
        //    mHeaderLoadingView.SetPadding(0, 0, 0, 0);
        //}
 
        //public void hideHeader()
        //{
        //    mHeaderLoadingView.SetPadding(0, 0, 0, -100);
        //}
 
 
        ////***************************上拉加载***************************
        //string _mFooterRefreshText = "正在加载...";
        //public string mFooterRefreshText
        //{
        //    get
        //    {
        //        return _mFooterRefreshText;
        //    }
        //    set
        //    {
        //        _mFooterRefreshText = value;
        //    }
        //}
 
        ///// <summary>
        ///// 是否支持 上拉加载
        ///// </summary>
        //bool _isCanFooterRefresh;
        //public bool IsCanFooterRefresh
        //{
        //    get
        //    {
        //        return _isCanFooterRefresh;
        //    }
        //    set
        //    {
        //        _isCanFooterRefresh = value;
        //    }
        //}
 
        ///// <summary>
        ///// 是否正在上拉加载
        ///// </summary>
        //bool _isFooterRefreshing;
        //public bool IsFooterRefreshing
        //{
        //    get
        //    {
        //        return _isFooterRefreshing;
        //    }
        //    set
        //    {
        //        _isFooterRefreshing = value;
        //    }
        //}
 
 
        ///// <summary>
        ///// 底部 开始上拉加载
        ///// </summary>
        //public void beginFooterRefreshing()
        //{
        //    if (!IsCanFooterRefresh) return;
 
        //    if (!IsFooterRefreshing)
        //    {
 
        //        mFooterLoadingView.Start(mHeaderRefreshText);
        //        IsFooterRefreshing = true;
        //        beginFooterRefreshingAction?.Invoke();
        //    }
        //}
 
        ///// <summary>
        ///// 底部 结束上拉加载
        ///// </summary>
        //public void endFooterRefreshing()
        //{
        //    if (!IsCanFooterRefresh) return;
 
        //    if (IsFooterRefreshing)
        //    {
        //        mFooterLoadingView.Hide();
        //        IsFooterRefreshing = false;
        //    }
        //}
 
 
 
 
 
 
    }

    /// <summary>
    /// 竖直方向滑动控件
    /// </summary>
    class AndroidRefreshScrolView : Android.Widget.ScrollView
    {
        /// <summary>
        /// 是否允许滑动
        /// </summary>
        public bool ScrollEnable = true;
 
        public Action AlreadyToTopAction;
        public Action AlreadyToTopUpAction;
        public Action AlreadyToBottomAction;
 
        //Scroller mScroller;


        //public void showHeader() {
 
        //    this.SetPadding(0, 0, 0, 0);
        //    ////mScroller.
        //    //mScroller.StartScroll(0, 0, 0, 0, 400);
 
        //    //// trigger computeScroll
        //    //Invalidate();
        //}
 
        //public void hideHeader()
        //{
        //    this.SetPadding(0, -50, 0, 0);
 
        //    ////mScroller.
        //    //mScroller.StartScroll(0, 40, 0, -40, 400);
 
        //    //// trigger computeScroll
        //    //Invalidate();
        //}


        View _view;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">Context.</param>
        /// <param name="view">View.</param>
        public AndroidRefreshScrolView(Context context, View view)
            : base(context)
        {
            _view = view;
            VerticalScrollBarEnabled = true;
            //mScroller = new Scroller(context, new DecelerateInterpolator());
        }
 
 
        bool bAlreadyToTop;

        public override bool DispatchTouchEvent(MotionEvent e)
        {
            //System.Console.WriteLine($"{GetType()} Height->{Height} Width->{Width} DispatchTouchEvent->{e.Action}");
            if (e.Action == MotionEventActions.Down)
            {
                //还原允许拦截事件
                RequestDisallowInterceptTouchEvent(false);
            }
            return base.DispatchTouchEvent(e);
        }
 
        float mLastX, mLastY ;
        public override bool OnInterceptTouchEvent(MotionEvent ev)
        {
            //System.Console.WriteLine($"{GetType()} Height->{Height} Width->{Width} OnInterceptTouchEvent->{ev.Action}");
 
            if (!ScrollEnable || disallowIntercept)
            {
                return false;
            }
 
            switch (ev.Action)
            {
                case MotionEventActions.Down:
                    mLastX = ev.RawX;
                    mLastY = ev.RawY;
                    bAlreadyToTop = false;
                    System.Console.WriteLine($"MotionEventActions.Down");
                    Parent?.RequestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEventActions.Up:
       
                    Parent?.RequestDisallowInterceptTouchEvent(true);
                    System.Console.WriteLine($"MotionEventActions.Up");
                    if (bAlreadyToTop)
                    {
                        AlreadyToTopUpAction?.Invoke();
                        System.Console.WriteLine($"AlreadyToTopUpAction");
                    }
                    break;
                case MotionEventActions.Move:
                    var x = ev.RawX;
                    var y = ev.RawY;
                    var deltaX = x - mLastX;
                    var deltaY = y - mLastY;
                    System.Console.WriteLine($"MotionEventActions.Move");

                    if (Math.Abs(deltaX) < Math.Abs(deltaY))
                    {
                        //向下滑动,如果当前已经滑动到顶,就允许父控件的拦截事件
                        if (deltaY > 0 && ScrollY == 0)
                        {
                            bAlreadyToTop = true;
                            AlreadyToTopAction?.Invoke();
                            Parent?.RequestDisallowInterceptTouchEvent(false);
 
                           
                         
                            return false;
                        }
                        //向上滑动时,如果translateY等于高度时,即表明滑动到底部,可又顶层View控制滑动
                        var mChildH = 0;
                        if (0 < ChildCount)
                        {
                            mChildH = GetChildAt(ChildCount - 1).Bottom;
                        }
                        var translateY = mChildH - ScrollY;
                        if (deltaY < 0 && translateY == Height)
                        {
                            AlreadyToBottomAction?.Invoke();
                            Parent?.RequestDisallowInterceptTouchEvent(false);
                            
                            return false;
                        }
                    }
                    else
                    {
                        //左右滑动时,允许父控件的拦截
                        if (4 < Math.Abs(deltaX))
                        {
                            Parent?.RequestDisallowInterceptTouchEvent(false);
                        }
                    }
                    break;
            }

            return base.OnInterceptTouchEvent(ev);
        }
        /// <summary>
        /// 重写点击事件
        /// </summary>
        /// <returns><c>true</c>, if touch event was oned, <c>false</c> otherwise.</returns>
        /// <param name="e">E.</param>
        public override bool OnTouchEvent(MotionEvent e)
        {
            //System.Console.WriteLine($"{GetType()} Height->{Height} Width->{Width} OnInterceptTouchEvent->{e.Action}");
            _view?.TouchEvent(e);
            base.OnTouchEvent(e);
            //如果返回真父控件就不需要处理了
            return true;
        }
        bool disallowIntercept;
        public override void RequestDisallowInterceptTouchEvent(bool disallowIntercept)
        {
            this.disallowIntercept = disallowIntercept;
            //System.Console.WriteLine($"{GetType()} Height->{Height} RequestDisallowInterceptTouchEvent->{disallowIntercept}");
            base.RequestDisallowInterceptTouchEvent(disallowIntercept);
            Parent?.RequestDisallowInterceptTouchEvent(disallowIntercept);
        }
    }
 
 
 
 
    class MyLoadingHorizontal : Android.Widget.LinearLayout
    {
        ProgressBar Progress;
        public Android.Widget.TextView textView;
        LinearLayout tempLinearLayout;
 
 
        public void ShowProgress()
        {
            Progress.Visibility = ViewStates.Visible;
 
        }
 
        public void HideProgress()
        {
            Progress.Visibility = ViewStates.Gone;
 
        }
        public MyLoadingHorizontal(Context context)
            : base(context)
        {
 
            Progress = new ProgressBar(context);
    
 
            //SetBackgroundColor (Android.Graphics.Color.Argb (50, 0x32, 0x32, 0x32));
            SetGravity(GravityFlags.Center);
            tempLinearLayout = new MiddleLinearLayout(context) { Orientation = Orientation.Horizontal };
            tempLinearLayout.SetGravity(GravityFlags.CenterHorizontal);
 
 
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WrapContent, Application.GetRealHeight(30));
            lp.Gravity = Android.Views.GravityFlags.Center;
            tempLinearLayout.AddView(Progress, lp);
      
 
 
            textView = new Android.Widget.TextView(context);
            textView.Text = "Loading.....";
            textView.Gravity = GravityFlags.Center;
            tempLinearLayout.AddView(textView, new Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.WrapContent, Application.GetRealHeight(40)));
            AddView(tempLinearLayout, new Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.WrapContent, Android.Views.ViewGroup.LayoutParams.WrapContent));
            tempLinearLayout.SetPadding(20, 20, 20, 20);
        }
 
        uint LoadingHorizontalBackgroundColor;
        /// <summary>
        /// 背景颜色
        /// </summary>
        /// <value>The color of the background.</value>
        public virtual uint LodingBackgroundColor
        {
            get
            {
                return LoadingHorizontalBackgroundColor;
            }
            set
            {
                LoadingHorizontalBackgroundColor = value;
 
                if (this.GetType() != typeof(ImageView))
                {
                    if (tempLinearLayout.Background == null || tempLinearLayout.Background.GetType() != typeof(GradientDrawable))
                    {
                        tempLinearLayout.Background = new Android.Graphics.Drawables.GradientDrawable();
                    }
 
                    var gradientDrawable = (GradientDrawable)tempLinearLayout.Background;
                    gradientDrawable.SetCornerRadius(DensityUtil.Dip2Px(6));
                    byte r, g, b, a;
 
                    r = (byte)(LoadingHorizontalBackgroundColor / 256 / 256 % 256);
                    g = (byte)(LoadingHorizontalBackgroundColor / 256 % 256);
                    b = (byte)(LoadingHorizontalBackgroundColor % 256);
                    a = (byte)(LoadingHorizontalBackgroundColor / 256 / 256 / 256 % 256);
                    gradientDrawable.SetColor(Android.Graphics.Color.Argb(a, r, g, b).ToArgb());
                }
            }
        }
 
        /// <summary>
        /// 点击事件
        /// </summary>
        /// <returns>The touch event.</returns>
        /// <param name="e">E.</param>
        public override bool OnTouchEvent(Android.Views.MotionEvent e)
        {
            //事件自己处理
            return true;
        }
    }
}