解決DrawerLayout不能全屏滑動的問題

前言

對于如下效果的側(cè)邊菜單欄,android SDK提供了DrawerLayout這個控件可以完美的實現(xiàn),但是有一個問題,使用這個控件之后只能從左側(cè)(或者右側(cè))邊緣滑動才能顯示菜單欄。


這里寫圖片描述

解決辦法

如果想要全屏實現(xiàn)右滑出現(xiàn)菜單,只需要如下兩步:

一、 添加下面的代碼

private void setDrawerLeftEdgeSize (Activity activity, DrawerLayout drawerLayout, float displayWidthPercentage) {
    if (activity == null || drawerLayout == null) return;
    try {
        // 找到 ViewDragHelper 并設(shè)置 Accessible 為true
        Field leftDraggerField =
                drawerLayout.getClass().getDeclaredField("mLeftDragger");//Right
        leftDraggerField.setAccessible(true);
        ViewDragHelper leftDragger = (ViewDragHelper) leftDraggerField.get(drawerLayout);

        // 找到 edgeSizeField 并設(shè)置 Accessible 為true
        Field edgeSizeField = leftDragger.getClass().getDeclaredField("mEdgeSize");
        edgeSizeField.setAccessible(true);
        int edgeSize = edgeSizeField.getInt(leftDragger);

        // 設(shè)置新的邊緣大小
        Point displaySize = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
        edgeSizeField.setInt(leftDragger, Math.max(edgeSize, (int) (displaySize.x *
                displayWidthPercentage)));
    } catch (NoSuchFieldException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    }
}

二、調(diào)用這個方法

第一個參數(shù)傳入當前的Activity,第二個參數(shù)傳入drawerlayout的對象,最后一個參數(shù) 傳 1,即可實現(xiàn)全屏滑動。如果你想讓右側(cè)菜單也是全屏,只要將對應(yīng)的 "mLeftDragger" 改為 "mRightDragger"。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容