using System; using Android.Widget; using Android.Views; using Android.Content; using Android.Util; using Android.Runtime; namespace Shared { //已经全面检查了代码 /// /// 位置布局 /// public class HorizontalScrolViewLayout:ViewGroup { /// /// 构造函数 /// 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; } /// /// 视图宽度 /// /// The width. public override int Width { get { return base.Width; } set { base.Width = value; if (!IsCanRefresh) { return; } realViewGroup.LayoutParameters.Width = Width; } } /// /// 视图高度 /// /// The height. public override int Height { get { return base.Height; } set { base.Height = value; if (!IsCanRefresh) { return; } realViewGroup.LayoutParameters.Height = Height; } } /// /// 是否允许滑动 /// /// true if scroll enabled; otherwise, false. public bool ScrollEnabled { get { return (viewGroup as AndroidHorizontalScrollView).ScrollEnabled; } set { (viewGroup as AndroidHorizontalScrollView).ScrollEnabled = value; } } /// /// 是否正在滑动 /// /// true if decelerating; otherwise, false. public bool Decelerating { get { return false; } } /// 
 /// 是否显示滚动条
 /// //bool _IsShowScrollBar; public bool HorizontalScrollBarEnabled { get
 {
 return (viewGroup as AndroidHorizontalScrollView).HorizontalScrollBarEnabled;
 } set { (viewGroup as AndroidHorizontalScrollView).HorizontalScrollBarEnabled = value; } } /// /// 滑动到指定View /// /// 指定view的索引值 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); } /// /// 滑动到指定位置 X /// /// public void ScrollToX(int viewX) { if (viewX < 0) { viewX = 0; } (viewGroup as AndroidHorizontalScrollView).ScrollTo(viewX, 0); } } /// /// Android 原生 AndroidHorizontalScrollView /// class AndroidHorizontalScrollView: Android.Widget.HorizontalScrollView { public bool ScrollEnabled=true; View _view; /// /// 构造函数 /// /// Context. /// View. public AndroidHorizontalScrollView(Context context,View view) : base(context) { _view = view; } /// /// 重写点击事件 /// /// true事件已经处理false事件没有处理 /// E. 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); } } }