簡體   English   中英

通過按鈕打開導航抽屜

[英]Open Navigation Drawer by button

我嘗試從活動中調用導航抽屜,但沒有任何反應。我應該使用ActionBarDrawerToggle嗎?

  public class SettingsDrawer {
    Context mContext;
    ArrayList<SettingsDrawerItem> items;
        ListView listView;
        ImageView logo;
         DrawerLayout mDrawerLayout;
        public  SettingsDrawer(Context context) {
            this.mContext = context;   
 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.settings_drawer_layout, null);
            mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawerLayout);
            listView = (ListView) view.findViewById(R.id.settingsList);
            logo = (ImageView) view.findViewById(R.id.logo);
            DrawListAdapter adapter = new DrawListAdapter(mContext, items);
            listView.setAdapter(adapter);
                mDrawerLayout.openDrawer(Gravity.LEFT);
        }

在我的活動中

public class Activity extends AppCompatActivity{

Button settingsDrawerButton =(Button)findViewById(R.id.settingsDrawer);
        settingsDrawerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG,"settings drawer click!");
                SettingsDrawer s = new SettingsDrawer(Activity.this);

            }
        });
}

更新:XML我試圖像這樣設置:mDrawerLayout.openDrawer(drawerPane)但我也得到了classCastException。 我認為這很不穩定,因為DrawerLayout無法轉換為RelativeLayout。

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


    <RelativeLayout
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:id="@+id/drawerPane"
        android:layout_gravity="start">


        <RelativeLayout
            android:id="@+id/logoBox"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/material_blue_grey_800"
            android:padding="8dp" >

            <ImageView
                android:id="@+id/logo"
                 android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_centerInParent="true"/>

        </RelativeLayout>

        <ListView
            android:id="@+id/settingsList"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/logoBox"
            android:choiceMode="singleChoice"
            android:background="#ffffffff" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

找到這樣的抽屜布局:

  myDrawerLayout = (DrawerLayout) getView().findViewById(R.id.yourdrawer);

變量myDrawerLayout應該是您的活動中的字段:

DrawerLayout myDrawerLayout;

然后最好在您的活動中創建這樣的方法:

public void openDrawer(){
myDrawerLayout.openDrawer(mDrawerLayout);

}

然后,只要您要關閉抽屜,就可以輕松調用此方法;)

mDrawerLayout.openDrawer(listView);

the below code is my xml 

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="mActivity">

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/mDrawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            <RelativeLayout
                android:id="@+id/mDrawerMainLayout"
                android:layout_width="340dp"
                android:layout_gravity="start"
                android:layout_height="match_parent">

                <ListView
                    android:id="@+id/mListView"
                    android:layout_width="340dp"
                    android:layout_height="match_parent"
                    android:background="@color/white"
                    android:choiceMode="singleChoice"
                    android:dividerHeight="1dp" />
            </RelativeLayout>
        </android.support.v4.widget.DrawerLayout>

    </RelativeLayout> `from that I fixed  private void` 

openAndColseDrawerList() {
        if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
            mDrawerLayout.closeDrawer(mDrawerMainLayout);
        } else
            mDrawerLayout.openDrawer(mDrawerMainLayout);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM