簡體   English   中英

ToolBar上的按鈕不會打開導航抽屜

[英]Button on ToolBar won't open the Navigation Drawer

我創建了一個包含導航抽屜的基本活動。

抽屜將在幻燈片上打開,但從不通過工具欄上的導航按鈕。 我已經堅持了很長一段時間,並且在創建這個基類之前讓它工作了,我不認為我在邏輯上改變了什么。

我知道之前已經問過這個問題,但是我已經完成了所有其他類似的帖子並且無法解決它。

提前致謝!

public class DrawerActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


protected DrawerLayout          drawerLayout;
private   ActionBarDrawerToggle toggle;
private   Toolbar               toolbar;
private   NavigationView        navigationView;
private   boolean               enableToolbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    enableToolbar  =  true;

}


@Override
public void setContentView(int layoutResID){
    super.setContentView(R.layout.activity_drawer);
    setToolbar();
    setDrawerLayout();
    setNavigationDrawer();
    setToolbarOnClickListener();
    if(!useToolbar())
        toolbar.setVisibility(View.GONE);
    inflateToContentFrame(layoutResID);
}


public void setUsesToolbar(boolean _enable){
    enableToolbar = _enable;
}


private boolean useToolbar(){
    return enableToolbar;
}


private void setToolbar(){
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}


private void setDrawerLayout(){
    drawerLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_drawer, null);
    toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close){

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            DrawerActivity.this.invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            DrawerActivity.this.invalidateOptionsMenu();
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            DrawerActivity.this.toolbar.setAlpha(1 - slideOffset / 2);
        }
    };

    toggle.setDrawerIndicatorEnabled(true);
    drawerLayout.addDrawerListener(toggle);
    drawerLayout.post(new Runnable() {
        @Override
        public void run() {
            toggle.syncState();
        }
    });

}

private void setNavigationDrawer(){
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}


private void setToolbarOnClickListener(){
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DrawerActivity.this.drawerLayout.openDrawer(GravityCompat.START);
        }
    });
}


private void inflateToContentFrame(int layoutResID){
    FrameLayout content = (FrameLayout) findViewById(R.id.content_frame);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(layoutResID, content, true);
}


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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.drawer, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();
    Intent _intent;
    if (id == R.id.nav_classes) {
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if (id == R.id.nav_appointments) {
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if (id == R.id.nav_clients) {
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if (id == R.id.nav_payments) {
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if(id == R.id.nav_Settings){
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if (id == R.id.nav_share) {
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if (id == R.id.nav_send) {
        _intent = new Intent(this, AppointmentsActivity.class);
    }else{
        _intent = new Intent(this, AppointmentsActivity.class);
    }

    drawerLayout.closeDrawer(GravityCompat.START);
    startActivity(_intent);
    return true;
}



}

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"
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">

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


<include
    layout="@layout/app_bar_drawer"
    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="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_drawer"
    app:menu="@menu/activity_drawer_drawer"
    />




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

您正在設置Toggle with的DrawerLayout實例不是屏幕上的實例。 setDrawerLayout()方法中,您正在為一個從未使用過的新布局進行充氣。

而不是在那里膨脹,使用findViewById()來獲取在super.setContentView()調用中創建並添加到ActivityDrawerLayout實例。

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

此外,您可以省略toolbar.setNavigationOnClickListener()調用,因為ActionBarDrawerToggle將自己設置一個偵聽器,並在內部處理打開和關閉抽屜。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM