using System;
using Android.Content;
using Android.Views;
using Android.Support.V4.View;
using Android.Widget;
using Android.Support.V4.Widget;
namespace Shared
{
///
/// 右滑抽屉View
///
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;
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);
}
///
/// 是否锁定右滑功能, 锁定后右滑弹出菜单失效
///
bool _IsDrawerLockMode;
public bool IsDrawerLockMode
{
get
{
return _IsDrawerLockMode;
}
set
{
_IsDrawerLockMode = value;
if (_IsDrawerLockMode)
{
androidDrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed);
}
else
{
androidDrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeUnlocked);
}
}
}
///
/// 打开抽屉view
///
public void OpenLeftMenu()
{
if (!androidDrawerLayout.IsDrawerOpen(GravityCompat.Start))
{
androidDrawerLayout.OpenDrawer(GravityCompat.Start);
}
//Shared.HDLUtils.WriteLine("DrawerLayout:OpenLeftMenu");
}
///
/// 关闭抽屉view
///
public void CloseLeftMenu()
{
if (androidDrawerLayout.IsDrawerOpen(GravityCompat.Start))
{
androidDrawerLayout.CloseDrawer(GravityCompat.Start);
}
//Shared.HDLUtils.WriteLine("DrawerLayout:CloseLeftMenu");
}
///
/// 增加左边视图
///
/// View.
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();
}
///
/// RemoveLeftView
///
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;
//}
/////
///// 执行OnInterceptTouchEvent和OnTouchEvent
/////
///// true事件已经处理false事件没有处理
///// E.
//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);
//}
/////
///// 重写点击事件
/////
///// true, if touch event was oned, false otherwise.
///// E.
//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);
//}
}
}
}