【问题标题】:Open Navigation Drawer by button通过按钮打开导航抽屉
【发布时间】:2016-10-11 11:04:26
【问题描述】:

我试图从我的活动中调用导航抽屉,但没有任何反应。我应该使用 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>

【问题讨论】:

  • 添加这一行 mDrawerLayout.openDrawer(listView);

标签: android navigation navigation-drawer


【解决方案1】:

找到这样的抽屉布局:

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

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

DrawerLayout myDrawerLayout;

那么最好在你的活动中使用这样的方法:

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

}

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

【讨论】:

  • 是的,但我想在特定的类中设置我的抽屉内容,然后从情侣活动中调用它以不在每个活动中重复代码
  • 你不能从另一个班级打开抽屉因为它属于这个活动。不过,您可以将此方法设为公共静态。顺便说一句,这实际上不是一个好习惯,但你可以做的一切。
  • 我知道,所以我想在每次去不同的活动时设置我的抽屉,这就是我在单独的类中实现 SettingsDrawer 的原因。问题是,我无法从我的活动类中调用抽屉
  • 你能把你说的其他类的代码放上来吗?
  • 我想我们有点误解了。 SettingsDrawer 设置该抽屉具有的所有内容。在几个活动中,我通过调用 SettingsDrawer s = new SettingsDrawer (activityName.this) 来设置我的抽屉
【解决方案2】:

mDrawerLayout.openDrawer(listView);

【讨论】:

  • 这会抛出 classCastException,尽管我认为这不是一个好主意,因为在我的 DrawerLayou 中,我不仅有一个列表,还有几个项目。
  • 你能发布你的 XML 部分吗?
  • 请像这样试试 myDrawerLayout.openDrawer(drawerPane);
  • 私有 void openAndColseDrawerList() { if (myDrawerLayout.isDrawerOpen(Gravity.LEFT)) { myDrawerLayout.closeDrawer(drawerPane); } 否则 myDrawerLayout.openDrawer(drawerPane); } 这对我有用,没有例外。!
【解决方案3】:
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);
    }

【讨论】:

  • 好吧,我想我知道问题出在哪里。 Activity 的布局 xml 与 setting_drawer_layout 不同。当我打开抽屉时,它会在不同的视图中触发,但什么也没发生。你知道如何让它在活动视图中触发吗?
  • 我认为你最好使用 SettingsDrawer 扩展片段并在你的活动中实现
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多