wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
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
using System;
using Android.Widget;
using Android.Views;
using Android.Content;
using Android.Util;
using Android.Runtime;
 
namespace Shared
{
    //已经全面检查了代码
    /// <summary>
    /// 位置布局
    /// </summary>
    public  class HorizontalScrolViewLayout:ViewGroup
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public HorizontalScrolViewLayout()
        {
            viewGroup = new AndroidHorizontalScrollView(Application.Activity,this);
            realViewGroup = new AndroidLinearLayout(Application.Activity,this);
            (realViewGroup as AndroidLinearLayout).Orientation = Orientation.Horizontal;
            viewGroup.AddView(realViewGroup);
            HorizontalScrollBarEnabled = false;
 
        }
 
        /// <summary>
        /// 视图宽度
        /// </summary>
        /// <value>The width.</value>
        public override int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                realViewGroup.LayoutParameters.Width = Width;
 
            }
        }
 
        /// <summary>
        /// 视图高度
        /// </summary>
        /// <value>The height.</value>
        public override int Height
        {
            get
            {
                return base.Height;
            }
            set
            {
                base.Height = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                realViewGroup.LayoutParameters.Height = Height;
            }
        }
 
        /// <summary>
        /// 是否允许滑动
        /// </summary>
        /// <value><c>true</c> if scroll enabled; otherwise, <c>false</c>.</value>
        public bool ScrollEnabled
        {
            get
            {
                return (viewGroup as AndroidHorizontalScrollView).ScrollEnabled;
            }
            set
            {
 
                (viewGroup as AndroidHorizontalScrollView).ScrollEnabled = value;
 
            }
        }
 
        /// <summary>
        /// 是否正在滑动
        /// </summary>
        /// <value><c>true</c> if decelerating; otherwise, <c>false</c>.</value>
        public bool Decelerating
        {
            get
            {
                return false;
            }
        }
 
        /// <summary>
        /// 是否显示滚动条
        /// </summary>
        //bool _IsShowScrollBar;
        public bool HorizontalScrollBarEnabled
        {
            get
            {
                return (viewGroup as AndroidHorizontalScrollView).HorizontalScrollBarEnabled;
            }
            set
            {
                (viewGroup as AndroidHorizontalScrollView).HorizontalScrollBarEnabled = value;
 
            }
        }
 
 
 
        /// <summary>
        /// 滑动到指定View
        /// </summary>
        /// <param name="viewIndex"> 指定view的索引值</param>
        public void ScrollToViewIndex(int viewIndex)
        {
            if (viewList.Count == 0)
                return;
            var viewX = this.viewList[viewIndex].X;
 
            if (viewX < 0)
            {
                viewX = 0;
            }
            (viewGroup as AndroidHorizontalScrollView).ScrollTo(viewX, 0);
        }
 
        /// <summary>
        /// 滑动到指定位置 X
        /// </summary>
        /// <param name="viewX"></param>
        public void ScrollToX(int viewX)
        {
            if (viewX < 0)
            {
                viewX = 0;
            }
            (viewGroup as AndroidHorizontalScrollView).ScrollTo(viewX, 0);
        }
 
 
 
    }
 
    /// <summary>
    /// Android 原生 AndroidHorizontalScrollView
    /// </summary>
    class AndroidHorizontalScrollView: Android.Widget.HorizontalScrollView
    {
        public bool ScrollEnabled=true;
        View _view;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">Context.</param>
        /// <param name="view">View.</param>
        public AndroidHorizontalScrollView(Context context,View view)
            : base(context) {
            _view = view;
        }
 
    
 
        /// <summary>
        /// 重写点击事件
        /// </summary>
        /// <returns><c>true</c>事件已经处理<c>false</c>事件没有处理</returns>
        /// <param name="e">E.</param>
        public override bool OnTouchEvent(MotionEvent e)
        {
            if (ScrollEnabled)
            {
                base.OnTouchEvent(e);
            }
            _view.TouchEvent( e);
            return true;
        }
 
        public override bool DispatchTouchEvent(MotionEvent e)
        {
            //Shared.HDLUtils.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)
        {
            //Shared.HDLUtils.WriteLine($"{GetType()} Height->{Height} Width->{Width} OnInterceptTouchEvent->{ev.Action}");
 
            if (!Enabled || disallowIntercept)
            {
                return false;
            }
 
            switch (ev.Action)
            {
                case MotionEventActions.Down:
                    mLastX = ev.RawX;
                    mLastY = ev.RawY;
                    Parent?.RequestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEventActions.Move:
                    var x = ev.RawX;
                    var y = ev.RawY;
                    var deltaX = x - mLastX;
                    var deltaY = y - mLastY;
 
                    //左右滑动
                    if (Math.Abs(deltaY) < Math.Abs(deltaX))
                    {
                        //向下滑动,如果当前已经滑动到顶,就允许父控件的拦截事件
                        if (deltaX > 0 && ScrollX == 0)
                        {
                            Parent?.RequestDisallowInterceptTouchEvent(false);
                            return false;
                        }
                        //向上滑动时,如果translateY等于高度时,即表明滑动到底部,可又顶层View控制滑动
                        var rightLoaction = 0;
                        if (0 < ChildCount)
                        {
                            rightLoaction = GetChildAt(ChildCount - 1).Right;
                        }
                        var translateX = rightLoaction - ScrollX;
                        if (deltaX < 0 && translateX == Width)
                        {
                            Parent?.RequestDisallowInterceptTouchEvent(false);
                            return false;
                        }
                    }
                    else
                    {
                        //上下滑动时,允许父控件的拦截
                        if (4 < Math.Abs(deltaY))
                        {
                            Parent?.RequestDisallowInterceptTouchEvent(false);
                        }
                    }
                    break;
            }
 
            return base.OnInterceptTouchEvent(ev);
        }
        bool disallowIntercept;
        public override void RequestDisallowInterceptTouchEvent(bool disallowIntercept)
        {
            this.disallowIntercept = disallowIntercept;
            //Shared.HDLUtils.WriteLine($"{GetType()} Height->{Height} RequestDisallowInterceptTouchEvent->{disallowIntercept}");
            base.RequestDisallowInterceptTouchEvent(disallowIntercept);
            Parent?.RequestDisallowInterceptTouchEvent(disallowIntercept);
        }
    }
 
}