DrawerLayout的默认灵敏度是可以的,但是我想让它更容易一些,以便在屏幕的一侧进行从左到右的滑动手势以打开抽屉式布局。我发现在平板电脑上,尤其是在某些手机上,它特别困难。

我在Web API的DrawerLayout页面中没有看到任何手势或手势 Controller 引用。

最佳答案

对于android.support.v4.widget.DrawerLayout,我想出了一个技巧,可让您扩展可开始滑动手势(中风)的区域,从而使滑动手势更容易。

Hack实现:

    // assuming mDrawerLayout is an instance of android.support.v4.widget.DrawerLayout
    try {

        // get dragger responsible for the dragging of the left drawer
        Field draggerField = DrawerLayout.class.getDeclaredField("mLeftDragger");
        draggerField.setAccessible(true);
        ViewDragHelper vdh = (ViewDragHelper)draggerField.get(mDrawerLayout);

        // get access to the private field which defines
        // how far from the edge dragging can start
        Field edgeSizeField = ViewDragHelper.class.getDeclaredField("mEdgeSize");
        edgeSizeField.setAccessible(true);

        // increase the edge size - while x2 should be good enough,
        // try bigger values to easily see the difference
        int origEdgeSize = (int)edgeSizeField.get(vdh);
        int newEdgeSize = (int) (origEdgeSize * 2);
        edgeSizeField.setInt(vdh, newEdgeSize);

    } catch (Exception e) {
        // we unexpectedly failed - e.g. if internal implementation of
        // either ViewDragHelper or DrawerLayout changed
    }

说明:

该破解基于这样一个事实,即DrawerLayout类依赖ViewDragHelper实例来处理拖动。有两种情况-一种用于左侧抽屉,一种用于右侧抽屉。他们被这样实例化:
 public DrawerLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    ... code omitted for brevity ..

    mLeftDragger = ViewDragHelper.create(this, TOUCH_SLOP_SENSITIVITY, mLeftCallback);
    mLeftDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    mLeftDragger.setMinVelocity(minVel);
    mLeftCallback.setDragger(mLeftDragger);

    mRightDragger = ViewDragHelper.create(this, TOUCH_SLOP_SENSITIVITY, mRightCallback);
    mRightDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_RIGHT);
    mRightDragger.setMinVelocity(minVel);
    mRightCallback.setDragger(mRightDragger);

    ... code omitted for brevity ..
}
mLeftDraggermRightDragger都是私有(private)字段,而mFieldSize中的ViewDragHelper字段也是私有(private)字段。没有公共(public)方法可以让客户端代码设置这些字段-因此,hack依赖于反射,而该反射对于ViewDragHelper和DrawerLayout类的内部实现更改是脆弱的。

奖励:
A comprehensive tutorial on ViewDragHelper usage

关于android - DrawerLayout修改灵敏度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17942223/

10-12 04:34