操作栏抽屉切换不会从右侧

时间:2017-09-19 14:24:48

标签: java android navigation-drawer android-toolbar android-drawer

我遇到了NavigationView和ActionBarDrawer Toggle的问题 当我触摸Toogle时,应用会关闭,这就是错误:

    Process: net.diginic.master, PID: 21770
                  java.lang.IllegalArgumentException: No drawer view found with 
    gravity LEFT
                      at 
    android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1651)
                      at 
    android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1637)
                      at

  android.support.v7.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:293)
                      at 

  android.support.v7.app.ActionBarDrawerToggle$1.onClick(ActionBarDrawerToggle.java:202)
                      at android.view.View.performClick(View.java:5637)
                      at android.view.View$PerformClick.run(View.java:22429)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

导航引力是正确的,重力离开没有问题 但我想用rtl汉堡包菜单图标打开导航并从右边打开

- MainActivity.java

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

    DrawerLayout drawerLayout;
    NavigationView navigationView;
    ActionBarDrawerToggle toggle;
    Toolbar toolbar;


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

        sliderLayout = (SliderLayout) findViewById(R.id.slider);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.layout_drawer);
        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        setSupportActionBar(toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
        drawerLayout.setDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);



    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.END)) {
            drawerLayout.closeDrawer(GravityCompat.END);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                    drawerLayout.closeDrawer(Gravity.RIGHT);
                } else {
                    drawerLayout.openDrawer(Gravity.RIGHT);
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.drawer_home:
                break;
            case R.id.drawer_sort_list:
                break;
            case R.id.drawer_shopping_cart:
                break;
            case R.id.drawer_most_buy:
                break;
            case R.id.drawer_most_see:
                break;
            case R.id.drawer_newest:
                break;
            case R.id.drawer_settings:
                break;
            case R.id.drawer_faq:
                break;
            case R.id.drawer_about:
                break;
        }
        drawerLayout.closeDrawer(Gravity.RIGHT);
        return true;
    }
}

- main_activity.xml

<?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"
    android:id="@+id/layout_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="end">

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

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true">

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layoutDirection="rtl"
            app:headerLayout="@layout/header"
            app:menu="@menu/main_drawer">

        </android.support.design.widget.NavigationView>
    </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

尝试在onCreate()

的底部添加此内容
toolbar.setNavigationOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    if (Drawer.isDrawerOpen(Gravity.END)) {
        Drawer.closeDrawer(Gravity.END);
    } else {
        Drawer.openDrawer(Gravity.END);
    }
}
});