0

使用自定义视图实现 ToolBar 控件:目标是使用 Xamarin.Android 框架实现滚动功能,而无需ActionBar根据我的业务需求使用。所需的功能是左右滑动、拖动工具栏按钮、投掷等。

滚动应该更准确,例如:如果用户从左到右或从右到左非常快速地滚动(或甩动)工具栏,它应该直接显示最后一个按钮。即类似于ActionBar大多数现代Android设备附带的。

请问实现此目的的正确回调方法是什么

高级设计考虑因素是: 工具栏按钮类实现ImageButton。ToolBar 按钮被设计为 ToolBarControl 类的内部类,它是实现 ViewGroup 的自定义视图,我现在不想使用ActionBar。我想重写OnMeasure,OnLayout方法并实现OnTouch()and OnFling()。覆盖OnInterceptTouchEvent(MotionEvent ev), OnTouchEvent(MotionEvent ev)。基于http://developer.android.com/training/gestures/index.html中的示例代码

代码片段:

//To Implement Scrolling
//to watch events by ViewGroup as they are dispatched to child Views
public override bool OnInterceptTouchEvent(MotionEvent e)
{
    //System.out.println(...)
    Console.WriteLine("OnInterceptTouchEvent");
    Boolean intercept = false;
    ViewConfiguration configuration = ViewConfiguration
                          .Get(Context);
    int mTouchSlop = configuration.ScaledTouchSlop;
    try
    {
        switch (e.Action)
        {
            case MotionEventActions.Move:
                {
                    int xDiff = (int)Math.Abs(e.GetX() - m_fLastX);
                    if (xDiff > mTouchSlop)
                    {
                        m_nTouchState = c_nTouchStateHorizontalScrolling;
                        m_fLastX = e.GetX();
                    }
                    int yDiff = (int)Math.Abs(e.GetY() - m_fLastY);
                    if (yDiff > mTouchSlop)
                    {
                        m_nTouchState = -1;
                    }
                    if (Math.Abs(xDiff * 2) > Math.Abs(yDiff) && xDiff > mTouchSlop)
                    {
                        intercept = true;
                    }

                    break;
                }

            case MotionEventActions.Cancel:
            case MotionEventActions.Up:
                // Release the drag.
                m_nTouchState = c_nTouchStateRest;
                break;
            case MotionEventActions.Down:
                m_fLastY = e.GetY();
                m_fLastX = e.GetX();
                break;
            default:
                break;
        }
    }
    catch (Exception ex)
    {
    }
    return intercept;
}

public override bool OnTouchEvent(MotionEvent e)
{

    //System.out.println(...)

    Console.WriteLine("TB OnTouchEvent");
    try
    {
        float x = e.GetX();
        float y = e.GetY();
        switch (e.Action)
        {
            case MotionEventActions.Down:
                if (!m_scroller.IsFinished)
                {
                    m_scroller.AbortAnimation();
                }
                m_fLastX = x;
                if (m_scroller.IsFinished)
                {
                    m_nTouchState = c_nTouchStateRest;
                }
                else
                {
                    m_nTouchState = c_nTouchStateHorizontalScrolling;
                }
                break;

            case MotionEventActions.Move:
                int deltaX = (int)(m_fLastX - x);
                m_fLastX = x;
                int scrollX = ScrollX;
                // Scroll to right
                if (deltaX < 0)
                {
                    if (scrollX > 0)
                    {
                        ScrollBy(Math.Max(-scrollX, deltaX), 0);
                    }
                }
                // Scroll to left
                else if (deltaX > 0)
                {
                    // Visible full icons always without empty spaces 
                    int availableToScroll =
                            GetChildAt(ChildCount - 1).Right - scrollX - (display.Width - c_nToolIndent);

                    if (availableToScroll > 0)
                    {
                        ScrollBy(Math.Min(availableToScroll, deltaX), 0);
                    }
                }
                break;
4

0 回答 0