按按钮打开导航抽屉

时间:2016-10-11 11:04:27

标签: android navigation navigation-drawer

我试图从我的活动中调用导航抽屉,但没有任何反应。我应该使用ActionBarDrawerToggle吗?

<distributed-scheme>

在我的活动中

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

更新:XML我尝试设置:mDrawerLayout.openDrawer(drawerPane),但我也有classCastException。我认为它非常明显,因为DrawerLayout无法转换为RelativeLayout。

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

            }
        });
}

3 个答案:

答案 0 :(得分:1)

找到这样的抽屉布局:

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

变量myDrawerLayout应该是您活动中的字段:

DrawerLayout myDrawerLayout;

然后更好地在你的活动中制作这样的方法:

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

}

然后在您想要关闭抽屉时轻松调用此方法;)

答案 1 :(得分:0)

mDrawerLayout.openDrawer(ListView中);

答案 2 :(得分:0)

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);
    }
相关问题