DrawerLayout导致慢速选择器

时间:2014-12-23 17:43:42

标签: android

我开始在我的应用程序中实现DrawerLayout,我注意到我的按钮的选择器响应非常慢。当我将DrawerLayout更改为LinearLayout时,滞后消失。我不确定这是否是由于DrawerLayout中发生的额外触摸处理导致此延迟或其他原因造成的。有什么办法可以减少这种滞后吗?

<android.support.v4.widget.DrawerLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/drawer_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <Button
         android:id="@+id/my_button"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:background="@drawable/button_background"
         android:text="" />

</android.support.v4.widget.DrawerLayout>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#000080"/>
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#0000ff"/>
        </shape>
    </item>
</selector>

1 个答案:

答案 0 :(得分:0)

DrawerLayout不会覆盖像LinearLayout这样的shouldDelayChildPressedState(),因此会产生延迟。必须创建这样的布局类:

public class MyDrawerLayout extends DrawerLayout {

    public MyDrawerLayout (Context context) {
        super(context);
    }

    public MyDrawerLayout (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyDrawerLayout (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean shouldDelayChildPressedState() {
        // prevents touch delay
        return false;
    }
}

我注意到的唯一副作用是滑动手势将在抽出抽屉之前选择视图。