gxc
2020-02-28 66a9965c44ecc32a6696abca876ab9d1cd091584
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
using System;
using Android.Views;
using Android.Content;
using Android.Widget;
using Android.Graphics.Drawables;
 
 
namespace Shared
{
    //已经全面检查了代码
    /// <summary>
    /// 位置布局
    /// </summary>
    public class HorizontalPages : ViewGroup
    {
        /// <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>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>
        public Action<HorizontalPages, int> PageChange;
        /// <summary>
        /// 构造函数
        /// </summary>
        public HorizontalPages()
        {
            viewGroup = new AndroidFrameLayout(Application.Activity, null);
            realViewGroup = new InnerHorizontalPages(Application.Activity, this) { };
            viewGroup.AddView(realViewGroup);
        }
 
        int pageIndex;
        /// <summary>
        /// 设置或者获取当前的界面索引
        /// </summary>
        /// <value>The index of the page.</value>
        public int PageIndex
        {
            get
            {
                return (realViewGroup as InnerHorizontalPages).CurrentScreen;
            }
            set
            {
                if (value < 0 || ChildrenCount < value)
                {
                    return;
                }
                pageIndex = value;
                if (!IsCanRefresh)
                {
                    return;
                }
 
                (realViewGroup as InnerHorizontalPages).SetSelection(value);
            }
        }
 
        /// <summary>
        /// 是否允许滑动
        /// </summary>
        /// <value><c>true</c> if scroll enabled; otherwise, <c>false</c>.</value>
        public bool ScrollEnabled
        {
            get
            {
                return (realViewGroup as InnerHorizontalPages).ScrollEnabled;
            }
            set
            {
                (realViewGroup as InnerHorizontalPages).ScrollEnabled = value;
            }
        }
 
 
 
        /// <summary>
        /// 增加子控件
        /// </summary>
        /// <param name="view">View.</param>
        public override void AddChidren(View view)
        {
            view.Parent = this;
            viewList.Add(view);
            realViewGroup.AddView(view.AndroidView);
 
            if (!IsCanRefresh)
            {
                return;
            }
 
            view.Refresh();
 
            if (view is ViewGroup)
            {
                var tempViewGroup = (ViewGroup)view;
                for (int i = 0; i < tempViewGroup.ChildrenCount; i++)
                {
                    tempViewGroup.GetChildren(i).Refresh();
                }
            }
        }
 
        /// <summary>
        /// 移除当前控件
        /// </summary>
        /// <param name="view">View.</param>
        internal override void Remove(View view)
        {
            if (view == null)
            {
                return;
            }
            var index = viewList.FindIndex(obj => obj == view);
            if (index < 0)
            {
                return;
            }
 
            viewList.Remove(view);
            (realViewGroup as InnerHorizontalPages).RemoveViewAt(index);
            view.Parent = null;
        }
 
        /// <summary>
        /// 移除所有的控件
        /// </summary>
        public override void RemoveAll()
        {
            while (0 < viewList.Count)
            {
                GetChildren(0)?.RemoveFromParent();
            }
            (realViewGroup as InnerHorizontalPages).SetSelection(0);
        }
 
        /// <summary>
        /// 根据索引移除控件
        /// </summary>
        /// <param name="index">Index.</param>
        public override void RemoveAt(int index)
        {
            if (viewList.Count - 1 < index || index < 0)
            {
                return;
            }
            GetChildren(index)?.RemoveFromParent();
        }
 
 
        /// <summary>
        /// page的外边距
        /// </summary>
        public int RowPadding
        {
             get
            {
                return (realViewGroup as InnerHorizontalPages).Padding;
            }
            set
            {
                (realViewGroup as InnerHorizontalPages).Padding = value;
 
            }
        }
 
        /// <summary>
        /// 两个Page之间的距离
        /// </summary>
        public int PagePadding
        {
            get
            {
                return (realViewGroup as InnerHorizontalPages).Padding - (realViewGroup as InnerHorizontalPages).PagePadding;
            }
            set
            {
                (realViewGroup as InnerHorizontalPages).PagePadding = (realViewGroup as InnerHorizontalPages).Padding - value;
 
            }
        }
    }
 
    public class InnerHorizontalPages : Android.Widget.FrameLayout
    {
        public bool ScrollEnabled = true;
        static int snapVelocity = 1000;
        static int invalidScreen = -1;
        static int touchStateRest = 0;
        static int touchStateScrolling = 1;
        
        /// <summary>
        /// 界面之间的边距
        /// </summary>
        public int Padding = 100;
        public int PagePadding = 50;
        Scroller scroller;
        VelocityTracker mVelocityTracker = VelocityTracker.Obtain();
        int touchState = touchStateRest;
        float lastMotionX;
        float lastMotionY;
        int mTouchSlop;
        int mMaximumVelocity;
        internal int CurrentScreen;
 
        public override void RemoveViewAt(int index)
        {
            if (index == ChildCount - 1) {
                CurrentScreen--;
                if (CurrentScreen < 0) {
                    CurrentScreen = 0;
                }
            }
            base.RemoveViewAt(index);
            
            //刷新
            snapToDestination();
        }
 
        Shared.HorizontalPages horizontalPages;
        public InnerHorizontalPages(Context context, Shared.HorizontalPages view) : base(context)
        {
            horizontalPages = view;
            init();
        }
 
        /// <summary>
        /// 重写这个是为了方便部局,在每个界面之间都多加了一个控件包住
        /// </summary>
        /// <param name="child"></param>
        public override void AddView(Android.Views.View child)
        {
            var innerView = new Android.Widget.FrameLayout(Shared.Application.Activity);
            innerView.SetPadding(Padding, 0, Padding, 0);
            if (1 <= ChildCount)
            {
                innerView.SetX(LayoutParameters.Width * ChildCount - Padding - PagePadding);
            }
            base.AddView(innerView, new Android.Widget.FrameLayout.LayoutParams(LayoutParameters.Width, LayoutParameters.Height) { });
            innerView.AddView(child);
        }
 
        void init()
        {
            scroller = new Scroller(Context);
            var configuration = ViewConfiguration.Get(Context);
            //最小的滑动距离
            mTouchSlop = configuration.ScaledTouchSlop;
            mMaximumVelocity = configuration.ScaledMaximumFlingVelocity;
        }
 
        public override void RequestDisallowInterceptTouchEvent(bool disallowIntercept)
        {
            base.RequestDisallowInterceptTouchEvent(disallowIntercept);
            disallowInterceptTouchEvent = disallowIntercept;
        }
        bool disallowInterceptTouchEvent;
 
        bool isFirst;
        public override bool OnInterceptTouchEvent(MotionEvent ev)
        {
            if (disallowInterceptTouchEvent)
            {
                return false;
            }
            System.Console.WriteLine($"PageLayout->OnInterceptTouchEvent:{Height}   {ev.Action}");
            //如果没有子控件或者不允许滑动就返回,不处理当前事件
            if (ChildCount == 0 || !ScrollEnabled)
                return false;
 
            mVelocityTracker.AddMovement(ev);
            var x = ev.GetX();
            var y = ev.GetY();
            switch (ev.Action)
            {
                //每次点击都执行这里
                case MotionEventActions.Down:
                    if (!scroller.IsFinished)
                    {
                        //如果还在滑动,但是用户重新点击了,就让控件马上停止
                        scroller.AbortAnimation();
                        snapToDestination(false);
                    }
                    //记录点击的最新X坐标
                    lastMotionX = x;
                    lastMotionY = y;
                    touchState = scroller.IsFinished ? touchStateRest : touchStateScrolling;
                    break;
                //有子控件时执行这里
                case MotionEventActions.Move:
                    var deltaX = (int)(lastMotionX - x);
                    var deltaY = (int)(lastMotionY - y);
                    //当前滑动是否已经超出了设定值
                    var xMoved = Math.Abs(deltaX) > mTouchSlop;
                    System.Console.WriteLine($"PageLayout->xMoved22:{xMoved }   {deltaX}");
 
                    if (xMoved && Math.Abs(deltaY) < Math.Abs(deltaX))
                    {
                        if (!isFirst)
                        {
                            touchState = touchStateScrolling;
                        }
                        isFirst = false;
                        System.Console.WriteLine($"PageLayout->xMoved33:{xMoved }   {deltaX}");
 
                    }
                    if (touchState == touchStateScrolling)
                    {
                        //记录最新的坐标,因为接下来控件已经滑动了
                        lastMotionX = x;
                        System.Console.WriteLine($"PageLayout->lastMotionX:{lastMotionX }   {touchState}");
                        //当前事件自己处理了,子控制不需要处理当前这事件
                        return true;
                    }
                    break;
                case MotionEventActions.Cancel:
                    touchState = touchStateRest;
                    mVelocityTracker.Clear();
                    break;
            }
            return false;
        }
 
        public override bool OnTouchEvent(MotionEvent e)
        {
            if (disallowInterceptTouchEvent)
            {
                return false;
            }
            System.Console.WriteLine($"PageLayout->OnTouchEvent:{Height}   {e.Action}");
 
            if (ChildCount == 0 || !ScrollEnabled)
                return false;
 
            mVelocityTracker.AddMovement(e);
 
            var x = e.GetX();
            var y = e.GetY();
            switch (e.Action)
            {
                case MotionEventActions.Move:
                    var deltaX = (int)(lastMotionX - x);
                    var deltaY = (int)(lastMotionY - y);
                    //当前滑动是否已经超出了设定值
                    var xMoved = Math.Abs(deltaX) > mTouchSlop;
 
                    System.Console.WriteLine($"PageLayout->xMoved:{xMoved }   {deltaX}");
 
                    if (xMoved && Math.Abs(deltaY) < Math.Abs(deltaX))
                    {
                        if (!isFirst)
                        {
                            touchState = touchStateScrolling;
                        }
                        isFirst = false;
                    }
 
                    if (touchState == touchStateScrolling)
                    {
                        lastMotionX = x;
                        return true;
                    }
                    break;
 
                case MotionEventActions.Up:
                    if (touchState == touchStateScrolling)
                    {
                        mVelocityTracker.ComputeCurrentVelocity(1000, mMaximumVelocity);
                        var velocityX = mVelocityTracker.XVelocity;
 
                        if (velocityX > snapVelocity && CurrentScreen > 0)
                        {
                            // 显示左边界面
                            snapToScreen(CurrentScreen - 1);
                        }
                        else if (velocityX < -snapVelocity && CurrentScreen < ChildCount - 1)
                        {
                            // 显示右边界面
                            snapToScreen(CurrentScreen + 1);
                        }
                    }
 
                    touchState = touchStateRest;
                    mVelocityTracker.Clear();
                    break;
                case MotionEventActions.Cancel:
                    touchState = touchStateRest;
                    mVelocityTracker.Clear();
                    break;
            }
            return true;
        }
 
        /// <summary>
        /// 滑动到指定的界面
        /// </summary>
        void snapToDestination(bool isDelay = true)
        {
            var whichScreen = (ScrollX + (MeasuredWidth / 2)) / MeasuredWidth;
 
            snapToScreen(whichScreen, isDelay);
        }
        /// <summary>
        /// 滑动到指定界面
        /// </summary>
        /// <param name="whichScreen">Which screen.</param>
        void snapToScreen(int whichScreen, bool isDelay = true)
        {
            if (!scroller.IsFinished)
            {
                scroller.AbortAnimation();
            }
            var beforePosition = CurrentScreen;
            whichScreen = Math.Max(0, Math.Min(whichScreen, ChildCount - 1));
            CurrentScreen = whichScreen;
            //还原这个控件之前的位置
            var newX = CurrentScreen * MeasuredWidth;
            var delta = newX - ScrollX;
            scroller.StartScroll(ScrollX, 0, delta, 0, isDelay ? 800 : 0);
            Invalidate();
            refreshPage();
 
            if (beforePosition != CurrentScreen)
            {
                horizontalPages.PageChange?.Invoke(horizontalPages, CurrentScreen);
            }
        }
 
        void refreshPage()
        {
            GetChildAt(CurrentScreen).SetX(LayoutParameters.Width * CurrentScreen);
 
            if (0 <= CurrentScreen - 1)
            {
                //让上一个界面向右出来一点
                GetChildAt(CurrentScreen - 1).SetX(LayoutParameters.Width * (CurrentScreen - 1) + Padding + PagePadding);
            }
            if (CurrentScreen + 1 <= ChildCount - 1)
            {
                //让下一个界面向左出来一点
                GetChildAt(CurrentScreen + 1).SetX(LayoutParameters.Width * (CurrentScreen + 1) - Padding - PagePadding);
            }
        }
        public override void ComputeScroll()
        {
            if (scroller.ComputeScrollOffset())
            {
                ScrollTo(scroller.CurrX, scroller.CurrY);
                PostInvalidate();
            }
            else if (CurrentScreen != invalidScreen)
            {
                //mCurrentScreen = Math.Max(0, Math.Min(mNextScreen, ChildCount - 1));
                //mNextScreen = invalidScreen;
                //postViewSwitched(mLastScrollDirection);
            }
        }
 
        public void SetSelection(int position)
        {
            if (position < 0)
            {
                return;
            }
            scroller.ForceFinished(true);
 
            position = Math.Min(position, ChildCount - 1);
 
            snapToScreen(position, false);
        }
 
        public override bool DispatchTouchEvent(MotionEvent e)
        {
            //System.Console.WriteLine($"PageLayout->DispatchTouchEvent:{Height}   {e.Action}");
            if (e.Action == MotionEventActions.Down)
            {
                //还原中断
                RequestDisallowInterceptTouchEvent(false);
                isFirst = true;
            }
            return base.DispatchTouchEvent(e);
        }
    }
}