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
324
325
using System;
using Android.Content;
using Android.Views;
using Android.Support.V4.View;
using Android.Widget;
using Android.Support.V4.Widget;
 
 
namespace Shared
{
    /// <summary>
    /// 右滑抽屉View
    /// </summary>
    public class UIDrawerLayout : ViewGroup
    {
        AndroidDrawerLayout androidDrawerLayout;
 
        /// <summary>
        /// 宽度设置或读取
        /// </summary>
        /// <value>The width.</value>
        public override int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
                if (!IsCanRefresh)
                {
                    return;
                }
                var layoutParameters = androidDrawerLayout.LayoutParameters;
                layoutParameters.Width = Width;
                androidDrawerLayout.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 = androidDrawerLayout.LayoutParameters;
                layoutParameters.Height = Height;
                androidDrawerLayout.LayoutParameters = layoutParameters;
            }
        }
 
        LinearLayout leftLinarLayout;
 
        Android.Widget.FrameLayout mainLinarLayout;
 
        /// <summary>
        /// 在滑动事件下,可显示隐藏些自定义视图
        /// </summary>
        public UIDrawerLayout()
        {
            //viewGroup = new Android.Widget.FrameLayout(Application.Activity);
            androidDrawerLayout = new AndroidDrawerLayout(Application.Activity, this) { };
 
            viewGroup = androidDrawerLayout;
 
            leftLinarLayout = new LinearLayout(Application.Activity) {  Alpha = 1.0f };  
            mainLinarLayout = new Android.Widget.FrameLayout(Application.Activity) { Alpha = 1.0f };
 
            //viewGroup.AddView(androidDrawerLayout, new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MatchParent, DrawerLayout.LayoutParams.MatchParent));
            realViewGroup = mainLinarLayout;
 
            DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MatchParent, DrawerLayout.LayoutParams.MatchParent);
            androidDrawerLayout.AddView(mainLinarLayout, lp);
 
 
            DrawerLayout.LayoutParams lp2 = new DrawerLayout.LayoutParams(Application.CurrentWidth, DrawerLayout.LayoutParams.MatchParent);
            lp2.Gravity = GravityCompat.Start;
            androidDrawerLayout.AddView(leftLinarLayout, lp2);
 
 
    
        }
 
        /// <summary>
        /// 是否锁定右滑功能, 锁定后右滑弹出菜单失效
        /// </summary>
        bool _IsDrawerLockMode;
        public bool IsDrawerLockMode
        {
            get
            {
                return _IsDrawerLockMode;
            }
            set
            {
                _IsDrawerLockMode = value;
                if (_IsDrawerLockMode)
                {
                    androidDrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed);
                }
                else
                {
                    androidDrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeUnlocked);
                }
            }
        }
  
 
 
        /// <summary>
        /// 打开抽屉view
        /// </summary>
        public void OpenLeftMenu()
        {
 
            if (!androidDrawerLayout.IsDrawerOpen(GravityCompat.Start))
            {
                androidDrawerLayout.OpenDrawer(GravityCompat.Start);
            }
            //Shared.HDLUtils.WriteLine("DrawerLayout:OpenLeftMenu");
        }
 
 
 
        /// <summary>
        /// 关闭抽屉view
        /// </summary>
        public void CloseLeftMenu()
        {
            if (androidDrawerLayout.IsDrawerOpen(GravityCompat.Start))
            {
                androidDrawerLayout.CloseDrawer(GravityCompat.Start);
            }
 
            //Shared.HDLUtils.WriteLine("DrawerLayout:CloseLeftMenu");
        }
 
 
 
 
 
 
        /// <summary>
        /// 增加左边视图
        /// </summary>
        /// <param name="view">View.</param>
        public void AddLeftView(View view)
        {
 
            view.Parent = this;
            if (view.AndroidView.Parent != null)
            {
                (view.AndroidView.Parent as Android.Views.ViewGroup)?.RemoveView(view.AndroidView);
            }
            leftLinarLayout.AddView(view.AndroidView, new Android.Views.ViewGroup.LayoutParams(leftLinarLayout.LayoutParameters.Width, leftLinarLayout.LayoutParameters.Height));
    
            //view.Width = leftLinarLayout.LayoutParameters.Width;
            //view.Height = leftLinarLayout.LayoutParameters.Height;
            //view.Refresh();
 
        }
 
        /// <summary>
        /// RemoveLeftView
        /// </summary>
        public void LeftViewRemoveAllViews()
        {
            if (leftLinarLayout == null) return;
            leftLinarLayout.RemoveAllViews();
        }
 
 
 
 
        class AndroidDrawerLayout : DrawerLayout
        {
            View _view;
            int mTouchSlop
            {
                get
                {
                    var configuration = ViewConfiguration.Get(Context);
                    //最小的滑动距离
                    return configuration.ScaledTouchSlop;
                }
            }
            public AndroidDrawerLayout(Context context, View view)
                : base(context)
            {
                _view = view;
      
            }
 
            //float mLastX, mLastY;
            ////internal bool isShowLeft;
            ////internal bool isShowRight;
            //public Action RightToLeftAction;
            //public Action LeftToRightAction;
            //bool isActioned;
            //bool action(MotionEvent e)
            //{
            //    switch (e.Action)
            //    {
            //        case MotionEventActions.Down:
            //            //记录点击的最新X坐标
            //            mLastX = e.RawX;
            //            mLastY = e.RawY;
            //            Parent?.RequestDisallowInterceptTouchEvent(true);
            //            break;
            //        case MotionEventActions.Move:
            //            var x = e.RawX;
            //            var y = e.RawY;
            //            var deltaX = x - mLastX;
            //            var deltaY = y - mLastY;
 
            //            if (Math.Abs(deltaY) < Math.Abs(deltaX))
            //            {
            //                if (!isActioned)
            //                {
            //                    //RightToLeftAction
            //                    if (deltaX < -mTouchSlop)
            //                    {
 
            //                        if (x / this.Width >= 0.5)
            //                        { 
 
            //                            isActioned = true;
            //                            //parentDelay(true);
            //                            Shared.HDLUtils.WriteLine("DrawerLayout:DispatchTouchEvent:RightToLeft");
            //                            RightToLeftAction?.Invoke();
            //                        }
            //                    }
            //                    //LeftToRightAction
            //                    if (mTouchSlop < deltaX)
            //                    {
            //                        if (x / this.Width <= 0.5)
            //                        {
            //                            isActioned = true;
            //                            //parentDelay(true);
            //                            Shared.HDLUtils.WriteLine("DrawerLayout:DispatchTouchEvent:LeftToRight");
            //                            LeftToRightAction?.Invoke();
            //                        }
            //                    }
            //                }
            //                return true;
            //            }
            //            else
            //            {
            //                //上下滑动时,允许父控件的拦截
            //                if (4 < Math.Abs(deltaY))
            //                {
            //                    Parent?.RequestDisallowInterceptTouchEvent(false);
            //                }
            //            }
            //            break;
            //    }
            //    return false;
            //}
 
            ///// <summary>
            ///// 执行OnInterceptTouchEvent和OnTouchEvent
            ///// </summary>
            ///// <returns><c>true</c>事件已经处理<c>false</c>事件没有处理</returns>
            ///// <param name="e">E.</param>
            //public override bool DispatchTouchEvent(MotionEvent e)
            //{
            //    //Shared.HDLUtils.WriteLine($"{GetType()} Height->{Height} Width->{Width} DispatchTouchEvent->{e.Action}");
            //    if (e.Action == MotionEventActions.Down)
            //    {
            //        isActioned = false;
            //        //还原允许拦截事件
            //        RequestDisallowInterceptTouchEvent(false);
            //    }
            //    return base.DispatchTouchEvent(e);
            //}
 
            //public override bool OnInterceptTouchEvent(MotionEvent e)
            //{
            //    //Shared.HDLUtils.WriteLine($"{GetType()} Height->{Height} Width->{Width} OnInterceptTouchEvent->{e.Action}");
            //    if (disallowIntercept)
            //    {
            //        return false;
            //    }
            //    if (action(e))
            //    {
            //        return true;
            //    }
            //    return base.OnInterceptTouchEvent(e);
            //}
 
            ///// <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)
            //{
            //    //Shared.HDLUtils.WriteLine($"{GetType()} Height->{Height} Width->{Width} OnTouchEvent->{e.Action}");
            //    if (!isActioned)
            //    {
            //        _view.TouchEvent(e);
            //    }
            //    action(e);
            //    return true;
            //}
            //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);
            //}
        }
    }
}