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