using System; using Android.Content; using Android.Views; using Android.Support.V4.View; using Android.Widget; namespace Shared { /// /// 在滑动事件下,可显示隐藏些自定义视图 /// public class UIDrawerLayout : ViewGroup { AndroidDrawerLayout androidDrawerLayout; /// /// 宽度设置或读取 /// /// The width. 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; } } /// /// 视图高度 /// /// The height. 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; /// /// 在滑动事件下,可显示隐藏些自定义视图 /// public UIDrawerLayout() { //viewGroup = new Android.Widget.FrameLayout(Application.Activity); androidDrawerLayout = new AndroidDrawerLayout(Application.Activity, this) { }; viewGroup = androidDrawerLayout; //androidDrawerLayout.LeftToRightAction += () => //{ // OpenLeftMenu(); //}; //androidDrawerLayout.RightToLeftAction += () => //{ // CloseLeftMenu(); //}; leftLinarLayout = new LinearLayout(Application.Activity) { Alpha = 1.0f }; mainLinarLayout = new Android.Widget.FrameLayout(Application.Activity) { Alpha = 1.0f }; //viewGroup.AddView(androidDrawerLayout, new Android.Support.V4.Widget.DrawerLayout.LayoutParams(Android.Support.V4.Widget.DrawerLayout.LayoutParams.MatchParent, Android.Support.V4.Widget.DrawerLayout.LayoutParams.MatchParent)); realViewGroup = mainLinarLayout; Android.Support.V4.Widget.DrawerLayout.LayoutParams lp = new Android.Support.V4.Widget.DrawerLayout.LayoutParams(Android.Support.V4.Widget.DrawerLayout.LayoutParams.MatchParent, Android.Support.V4.Widget.DrawerLayout.LayoutParams.MatchParent); androidDrawerLayout.AddView(mainLinarLayout, lp); Android.Support.V4.Widget.DrawerLayout.LayoutParams lp2 = new Android.Support.V4.Widget.DrawerLayout.LayoutParams(Application.CurrentWidth, Android.Support.V4.Widget.DrawerLayout.LayoutParams.MatchParent); lp2.Gravity = GravityCompat.Start; androidDrawerLayout.AddView(leftLinarLayout, lp2); //lp2.Gravity = GravityCompat.End; //androidDrawerLayout.AddView(leftLinarLayout, lp2); } /// /// 打开抽屉view /// public void OpenLeftMenu() { if (!androidDrawerLayout.IsDrawerOpen(GravityCompat.Start)) { androidDrawerLayout.OpenDrawer(GravityCompat.Start); } System.Console.WriteLine("DrawerLayout:OpenLeftMenu"); } /// /// 关闭抽屉view /// public void CloseLeftMenu() { if (androidDrawerLayout.IsDrawerOpen(GravityCompat.Start)) { androidDrawerLayout.CloseDrawer(GravityCompat.Start); } System.Console.WriteLine("DrawerLayout:CloseLeftMenu"); } /// /// 增加左边视图 /// /// View. public void AddLeftView(View view) { leftLinarLayout.AddView(view.AndroidView, new Android.Views.ViewGroup.LayoutParams(leftLinarLayout.LayoutParameters.Width, leftLinarLayout.LayoutParameters.Height)); view.Parent = this; view.Width = leftLinarLayout.LayoutParameters.Width; view.Height = leftLinarLayout.LayoutParameters.Height; } class AndroidDrawerLayout : Android.Support.V4.Widget.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); // System.Console.WriteLine("DrawerLayout:DispatchTouchEvent:RightToLeft"); // RightToLeftAction?.Invoke(); // } // } // //LeftToRightAction // if (mTouchSlop < deltaX) // { // if (x / this.Width <= 0.5) // { // isActioned = true; // //parentDelay(true); // System.Console.WriteLine("DrawerLayout:DispatchTouchEvent:LeftToRight"); // LeftToRightAction?.Invoke(); // } // } // } // return true; // } // else // { // //上下滑动时,允许父控件的拦截 // if (4 < Math.Abs(deltaY)) // { // Parent?.RequestDisallowInterceptTouchEvent(false); // } // } // break; // } // return false; //} ///// ///// 执行OnInterceptTouchEvent和OnTouchEvent ///// ///// true事件已经处理false事件没有处理 ///// E. //public override bool DispatchTouchEvent(MotionEvent e) //{ // //System.Console.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) //{ // //System.Console.WriteLine($"{GetType()} Height->{Height} Width->{Width} OnInterceptTouchEvent->{e.Action}"); // if (disallowIntercept) // { // return false; // } // if (action(e)) // { // return true; // } // return base.OnInterceptTouchEvent(e); //} ///// ///// 重写点击事件 ///// ///// true, if touch event was oned, false otherwise. ///// E. //public override bool OnTouchEvent(MotionEvent e) //{ // //System.Console.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; // //System.Console.WriteLine($"{GetType()} Height->{Height} RequestDisallowInterceptTouchEvent->{disallowIntercept}"); // base.RequestDisallowInterceptTouchEvent(disallowIntercept); // Parent?.RequestDisallowInterceptTouchEvent(disallowIntercept); //} } } }