Android-NavigationView from right to left

安稳与你 提交于 2019-12-01 22:21:53

First of all replace on DrawerLayout the tools:openDrawer="start" with tools:openDrawer="end". Now your problem is on the toggle, which opens the left drawer, and because you have only right drawer it throws the exception. You can add your own button on the actionbar (on the right side) to open the drawer. To do that change your app_bar.xml like this

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

                <FrameLayout
                    android:id="@+id/drawer_button"
                    android:layout_width="50dp"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_alignParentRight="true"
                    android:clickable="true">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:src="@mipmap/ic_drawer" />
                </FrameLayout>


        </RelativeLayout>


    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

Then change your onCreate method from your Activity like this

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    findViewById(R.id.drawer_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // open right drawer
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.openDrawer(GravityCompat.END);
        }
    });

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

Also you can find the image for your drawer here or here. I hope this will help you. :)

use a custom drawerLayout just like this :

public class TaskManagerDrawerLayout  extends DrawerLayout {

public TaskManagerDrawerLayout(Context context) {
    super(context);
}

public TaskManagerDrawerLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TaskManagerDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return false;
    }
}

@Override
public void closeDrawer(int gravity) {
    super.closeDrawer(GravityCompat.END);
}

@Override
public void openDrawer(int gravity) {
    super.openDrawer(GravityCompat.END);
}

}
Mahalakshmi

After implementing left side navigation drawer change the following properties.

tools:openDrawer="start" in DrawerLayout
android:layout_gravity="end" in NavigationView  
toolbar.setNavigationIcon (R.color.transparent); mDrawerToggle.setDrawerIndicatorEnabled (false);

toolbar.setNavigationOnClickListener (new View.OnClickListener () { @Override public void onClick(View view) { if (mDrawerLayout.isDrawerOpen (Gravity.END)) { mDrawerLayout.closeDrawer (Gravity.END); } else { mDrawerLayout.openDrawer (Gravity.END); } } });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!