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
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
using System;
using Android.Content;
using Android.Views;
using Android.Views.Animations;
 
namespace Shared
{
    public class SwipeLayout:ViewGroup
    {
        AndroidSwipeLayout androidSwipeLayout;
 
        uint lineColor = 0x62818181;
        /// <summary>
        /// 背景颜色
        /// </summary>
        /// <value>The color of the background.</value>
        public virtual uint LineColor
        {
            get
            {
                return lineColor;
            }
            set
            {
                lineColor = value;
                byte a, r, g, b;
                a = (byte)(lineColor / 256 / 256 / 256 % 256);
                r = (byte)(lineColor / 256 / 256 % 256);
                g = (byte)(lineColor / 256 % 256);
                b = (byte)(lineColor % 256);
                lineLinearLayout.SetBackgroundColor(Android.Graphics.Color.Argb(a, r, g, b));
            }
        }
 
        /// <summary>
        /// 宽度设置或读取
        /// </summary>
        /// <value>The width.</value>
        public override int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                var layoutParameters = androidSwipeLayout.LayoutParameters;
                layoutParameters.Width = Width;
                androidSwipeLayout.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 = androidSwipeLayout.LayoutParameters;
                layoutParameters.Height = Height;
                androidSwipeLayout.LayoutParameters = layoutParameters;
            }
        }
 
        Android.Widget.LinearLayout leftLinarLayout;
        Android.Widget.LinearLayout rightLinarLayout;
        Android.Widget.LinearLayout lineLinearLayout;
        /// <summary>
        /// 在滑动事件下,可显示隐藏些自定义视图
        /// </summary>
        public SwipeLayout()
        {
            viewGroup = new Android.Widget.FrameLayout(Application.Activity);
            androidSwipeLayout = new AndroidSwipeLayout(Application.Activity, this);
            realViewGroup = androidSwipeLayout;
            leftLinarLayout = new Android.Widget.LinearLayout(Application.Activity) { Orientation = Android.Widget.Orientation.Horizontal, Alpha = 0.0f };
            rightLinarLayout = new Android.Widget.LinearLayout(Application.Activity) { Orientation = Android.Widget.Orientation.Horizontal, Alpha = 0.0f };
            lineLinearLayout = new Android.Widget.LinearLayout(Application.Activity);
            LineColor = lineColor;
 
            viewGroup.AddView(leftLinarLayout, new Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.WrapContent, Android.Views.ViewGroup.LayoutParams.MatchParent));
            viewGroup.AddView(rightLinarLayout, new Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.WrapContent, Android.Views.ViewGroup.LayoutParams.MatchParent));
            viewGroup.AddView(androidSwipeLayout, new Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, Android.Views.ViewGroup.LayoutParams.MatchParent));
            viewGroup.AddView(lineLinearLayout, new Android.Widget.FrameLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, DensityUtil.Dip2Px(1), Android.Views.GravityFlags.Bottom));
 
 
        }
 
        void openLeftMenu()
        {
            hideRightMenu();
 
            if (leftLinarLayout.ChildCount == 0)
                return;
 
            Android.Widget.FrameLayout.LayoutParams lp = (Android.Widget.FrameLayout.LayoutParams)androidSwipeLayout.LayoutParameters;
            lp.LeftMargin = leftLinarLayout.LayoutParameters.Width;
            androidSwipeLayout.LayoutParameters = lp;
            leftLinarLayout.Alpha = 1.0f;
 
            leftLinarLayout.ClearAnimation();
            leftLinarLayout.Animation = new TranslateAnimation(
                Dimension.RelativeToSelf, 0.1f,
                Dimension.RelativeToSelf, 0,
                Dimension.RelativeToSelf, 0,
                Dimension.RelativeToSelf, 0);
            leftLinarLayout.Animation.Duration = 200;
 
            leftLinarLayout.Animation.StartNow();
        }
 
        void hideLeftMenu()
        {
            Android.Widget.FrameLayout.LayoutParams lp = (Android.Widget.FrameLayout.LayoutParams)androidSwipeLayout.LayoutParameters;
            lp.LeftMargin = 0;
            androidSwipeLayout.LayoutParameters = lp;
            leftLinarLayout.Alpha = 0.0f;
        }
 
        void openRightMenu()
        {
            hideLeftMenu();
            if (rightLinarLayout.ChildCount == 0)
                return;
 
            Android.Widget.FrameLayout.LayoutParams lp = (Android.Widget.FrameLayout.LayoutParams)androidSwipeLayout.LayoutParameters;
            lp.LeftMargin = -rightLinarLayout.LayoutParameters.Width;
            androidSwipeLayout.LayoutParameters = lp;
 
            rightLinarLayout.Alpha = 1.0f;
 
            if (rightLinarLayout.Animation == null)
            {
                rightLinarLayout.Animation = new TranslateAnimation(
                        Dimension.RelativeToSelf, -0.1f,
                        Dimension.RelativeToSelf, 0.0f,
                        Dimension.RelativeToSelf, 0,
                        Dimension.RelativeToSelf, 0);
                rightLinarLayout.Animation.Duration = 200;
            }
 
            rightLinarLayout.Animation.Reset();
            rightLinarLayout.Animation.StartNow();
        }
 
        void hideRightMenu()
        {
            Android.Widget.FrameLayout.LayoutParams lp = (Android.Widget.FrameLayout.LayoutParams)androidSwipeLayout.LayoutParameters;
            lp.LeftMargin = 0;
            androidSwipeLayout.LayoutParameters = lp;
 
            rightLinarLayout.Alpha = 0.0f;
        }
 
        /// <summary>
        /// 子视图的宽度
        /// </summary>
        int subViewWidth = 100;
 
        /// <summary>
        /// 增加左边视图
        /// </summary>
        /// <param name="view">View.</param>
        public void AddLeftView(View view)
        {
            leftLinarLayout.AddView(view.AndroidView, new Android.Views.ViewGroup.LayoutParams(DensityUtil.Dip2Px(subViewWidth), androidSwipeLayout.LayoutParameters.Height));
            leftLinarLayout.LayoutParameters.Width = leftLinarLayout.ChildCount * DensityUtil.Dip2Px(subViewWidth);
            view.Parent = this;
            view.Width = DensityUtil.Dip2Px(subViewWidth);
            view.Height = androidSwipeLayout.LayoutParameters.Height;
        }
 
        /// <summary>
        /// 增加右边视图
        /// </summary>
        /// <param name="view">View.</param>
        public void AddRightView(View view)
        {
            rightLinarLayout.AddView(view.AndroidView, new Android.Views.ViewGroup.LayoutParams(DensityUtil.Dip2Px(subViewWidth), androidSwipeLayout.LayoutParameters.Height));
            rightLinarLayout.LayoutParameters.Width = rightLinarLayout.ChildCount * DensityUtil.Dip2Px(subViewWidth);
            ((Android.Widget.FrameLayout.LayoutParams)rightLinarLayout.LayoutParameters).LeftMargin = androidSwipeLayout.LayoutParameters.Width - rightLinarLayout.LayoutParameters.Width;
            view.Parent = this;
            view.Width = DensityUtil.Dip2Px(subViewWidth);
            view.Height = androidSwipeLayout.LayoutParameters.Height;
        }
 
        internal class AndroidSwipeLayout : Android.Widget.FrameLayout, GestureDetector.IOnGestureListener
        {
            /// <summary>
            /// 左右滑动的最短距离
            /// </summary>
            int distance = 100;
            /// <summary>
            /// 左右滑动的最小速度
            /// </summary>
            int velocity = 200;
 
            bool isFling;
            /// <summary>
            /// 手势对象
            /// </summary>
            GestureDetector mGesture;
            SwipeLayout swipeLayout;
            public AndroidSwipeLayout(Context context, SwipeLayout swipeLayout)
                : base(context)
            {
                this.swipeLayout = swipeLayout;
                mGesture = new GestureDetector(context, this);
            }
 
 
 
            public override bool OnTouchEvent(MotionEvent e)
            {
                if (Shared.AndroidView.IsLive(this) && !isFling)
                    swipeLayout.TouchEvent(e);
                return true;
            }
 
            public override bool OnInterceptTouchEvent(MotionEvent ev)
            {
                if (!Shared.AndroidView.IsLive(this))
                {
                    return false;
                }
                mGesture.OnTouchEvent(ev);
                //如果是滑动,就拦截事件,后面的事件是由自己的TouchEvent处理,子控件会收到取消事件,如果是快速滑动,事件也是自己处理
                if (isFling)
                {
                    return true;
                }
                //如果是滑动这里也会返回True
                return base.OnInterceptTouchEvent(ev);
            }
 
            /// <summary>
            /// 设置所有父视图禁止拦截事件
            /// </summary>
            /// <param name="b">如果设置True表示不要拦截,如果设置为False表示可以拦截</param>
            void requestParentDisallowInterceptTouchEvent(bool b)
            {
                var tempParent = Parent;
                while (tempParent != null)
                {   //告诉父类不要拦截这个视图的事件
                    tempParent.RequestDisallowInterceptTouchEvent(b);
                    tempParent = tempParent.Parent;
                }
            }
 
            public bool OnDown(MotionEvent e)
            {
                isFling = false;
                return false;
            }
 
            public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
            {
                // e1:第1个ACTION_DOWN MotionEvent
                // e2:最后一个ACTION_MOVE MotionEvent
                // velocityX:X轴上的移动速度(像素/秒)
                // velocityY:Y轴上的移动速度(像素/秒)
                // 向左滑
                if (e1.GetX() - e2.GetX() > distance
                    && Math.Abs(velocityX) > velocity)
                {
                    isFling = true;
                    swipeLayout.openRightMenu();
                }
                // 向右滑
                if (e2.GetX() - e1.GetX() > distance
                    && Math.Abs(velocityX) > velocity)
                {
                    isFling = true;
                    swipeLayout.openLeftMenu();
                }
                //迅速滑动,并松开  
                return false;
            }
 
            public void OnLongPress(MotionEvent e)
            {
                Shared.HDLUtils.WriteLine("OnLongPress:长按不放");
            }
 
            public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
            {
                Shared.HDLUtils.WriteLine("OnScroll:在屏幕上滑动");
                return false;
            }
 
            public void OnShowPress(MotionEvent e)
            {
                Shared.HDLUtils.WriteLine("OnShowPress:手指按下一段时间,还没到长按");
            }
 
            public bool OnSingleTapUp(MotionEvent e)
            {
                if(!isFling){
                    swipeLayout.hideLeftMenu();
                    swipeLayout.hideRightMenu();
                }
                Shared.HDLUtils.WriteLine("OnSingleTapUp:手指弹起");
                return false;
            }
        }
    }
}