简体   繁体   English

通过按钮打开导航抽屉

[英]Open Navigation Drawer by button

I tried to called navigation drawer from my activity, but nothing happened.Should I used ActionBarDrawerToggle? 我尝试从活动中调用导航抽屉,但没有任何反应。我应该使用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);
        }

And in my activity 在我的活动中

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);

            }
        });
}

UPDATE: XML I tried to set up like that: mDrawerLayout.openDrawer(drawerPane) but Ive got classCastException as well. 更新:XML我试图像这样设置:mDrawerLayout.openDrawer(drawerPane)但我也得到了classCastException。 I thought it was quite understable because the DrawerLayout cannot casts to RelativeLayout. 我认为这很不稳定,因为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>

Find the drawer layout like this: 找到这样的抽屉布局:

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

The variable myDrawerLayout should be field in your activity: 变量myDrawerLayout应该是您的活动中的字段:

DrawerLayout myDrawerLayout;

Then its Better to make a method like this in your activity: 然后最好在您的活动中创建这样的方法:

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

} }

Then easily call this method whever you want to close the drawer ;) 然后,只要您要关闭抽屉,就可以轻松调用此方法;)

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