安卓 - 右侧滑动菜单导航是否可行?

68

请查看此答案:https://dev59.com/1XTYa4cB1Zd3GeqPycAt#17792254 对我有效。 - Rudi
11个回答

69

NavigationDrawer可以配置为从左侧、右侧或两侧拉出。关键是在XML声明中抽屉的出现顺序以及layout_gravity属性的设置。以下是一个示例:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false" >
    </FrameLayout>

    <!-- Left drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice" />

    <!-- Right drawer -->

    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>

48

为什么他们的例子在我的情况下不起作用?我的意思是,我将它设置为希伯来语,但看不到它向右侧移动。 - android developer
我需要更多的信息才能继续。你确定一切都配置正确了吗?我建议您发布一个新问题。 - Larry McKenzie
我决定放弃它。我通过使用其他东西成功地让他们的项目工作,然后我决定不使用它,因为在另一侧有导航抽屉可能会令人困惑。也许我以后会改变主意。 - android developer
@LarryMcKenzie - 可以将可绘制菜单设置在右侧吗? - RoCkDevstack
@RoCk 我猜你在谈论那个看起来像三条水平线的汉堡菜单图标。它不是 DrawerLayout 的组件。因此,您可以在任何地方创建一个按钮,以打开导航抽屉。这不是一个推荐的模式,但您可以将该图标放在菜单中,这将使其位于工具栏的右侧。 - Larry McKenzie

25

我的应用崩溃了,报错信息为“找不到位置在 LEFT 的抽屉视图”。

于是我将以下内容添加到 onOptionsItemSelected 方法中:

if (item != null && item.getItemId() == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
            mDrawerLayout.closeDrawer(Gravity.RIGHT);
        } else {
            mDrawerLayout.openDrawer(Gravity.RIGHT);
        }
    }

5

https://dev59.com/d2Qn5IYBdhLWcg3wCjX0#21781710的解决方案基础上,还需要进行以下操作。

如果您正在使用由Android Studio创建的导航抽屉项目,则onOptionsItemSelected中的内容将会有所改变。因为他们创建了子类,所以您需要使用以下代码:

if (item != null && id == android.R.id.home) {
        if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
            mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
        } else {
            mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
        }
        return true;
}

下一步。在类NavigationDrawerFragment中,您需要创建3个方法:
方法1
public boolean isDrawerOpen(int gravity) {
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}

方法二
public void closeDrawer(int gravity) {
    mDrawerLayout.closeDrawer(gravity);
}

方法三
public void openDrawer(int gravity) {
    mDrawerLayout.openDrawer(gravity);
}

只有现在,右侧抽屉才能起作用。

3
您可以使用 Material design 中的 NavigationView。例如:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

1

从右侧实现导航抽屉很容易,比看起来简单得多。 在我看来,最简单的解决方案是:

  1. Extend DrawerLayout class and override open() and close() functions as below

     class EndDrawerLayout : DrawerLayout {
         constructor(context: Context) : super(context)
         constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
         constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
         override fun open() = openDrawer(GravityCompat.END)
         override fun close() = closeDrawer(GravityCompat.END)
     }
    
  2. Use the custom DrawerLayout in your XML

  3. Set NavigationView attribute android:layout_gravity="end"

     <com.custom.myapplication.ui.EndDrawerLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/drawer_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fitsSystemWindows="true"
         tools:openDrawer="end">
    
     <include
         android:id="@+id/app_bar_main"
         layout="@layout/app_bar_main"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
    
     <com.google.android.material.navigation.NavigationView
         android:id="@+id/nav_view"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_gravity="end"
         android:fitsSystemWindows="true"
         app:headerLayout="@layout/nav_header_main"
         app:menu="@menu/activity_main_drawer" />
    
     </com.custom.myapplication.ui.EndDrawerLayout>
    
  4. Enjoy


1

然后使用这些代码@amal,我认为这会对你有所帮助。 XML:

<!-- Framelayout to display Fragments -->

<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@drawable/counter_bg" >

        <ImageView
            android:id="@+id/iconl"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/iconr"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="17dp"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>
</FrameLayout>

<!-- Listview to display slider menu -->

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

<ListView
    android:id="@+id/list_slidermenu2"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

//设置所需图片

活动代码:

ImageView iconl,iconr;

private DrawerLayout mDrawerLayout;
private ListView mDrawerList,mDrawerList2;

ImageView iconl,iconr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iconl = (ImageView)findViewById(R.id.iconl);
    iconr = (ImageView)findViewById(R.id.iconr);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
    mDrawerList2 = (ListView) findViewById(R.id.list_slidermenu2);
    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
    mDrawerList2.setOnItemClickListener(new SlideMenuClickListener());
    iconl.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDrawerLayout.openDrawer(Gravity.START);
            mDrawerLayout.closeDrawer(Gravity.END);
        }
    });
    iconr.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDrawerLayout.openDrawer(Gravity.END);
            mDrawerLayout.closeDrawer(Gravity.START);
        }
    });
}
}

在这里,您可以为两个列表设置自己的列表适配器,并在项目单击时调用displayView(position)方法,在其中将您的片段添加到framelayout中。

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;


    default:
        break;
    }

    if (fragment != null) 
    {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
        .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);

        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

1

1
将导航抽屉设置在屏幕右侧,需要将抽屉布局作为导航视图的父布局,并将导航视图的布局重力设置为右侧。

1
将以下代码写入Main.java文件中,并且im1是在xml文件中顶部右侧的导航栏图标。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    im1=findViewById(R.id.humburgericon);
    im1.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("WrongConstant")
        @Override
        public void onClick(View v) {
              drawerLayout.openDrawer(Gravity.END);});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接