尽管我为导航抽屉按钮分配了方法,但它却无法点击

时间:2019-10-21 07:13:59

标签: android

尽管我分配了必需的方法,但单击我的导航抽屉图标后,它却无法运行, 从左滑动是有效的,但单击时无效。 我花了很多时间,但没有走错地方。 我尝试了setToolbarNavigationClickListener仍然无法工作。 有谁能够帮助我? 非常感谢您的帮助。:)

package com.example.myapplication;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private NavigationView navigation;

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

    navigation = findViewById(R.id.nav_view);
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            int id = menuItem.getItemId();
            switch (id) {
                case R.id.nav_cd:
                    Intent intent = new Intent(MainActivity.this, contact_developer1.class);
                    MainActivity.this.startActivity(intent);
                    return true;
                case R.id.nav_atapp:
                    Intent intent1 = new Intent(MainActivity.this, about_the_app.class);
                    startActivity(intent1);
                    return true;
                case R.id.nav_atchurch:
                    Intent intent2 = new Intent(MainActivity.this, about_the_church.class);
                    startActivity(intent2);
                    break;
                case R.id.nav_ods:
                    Intent intent3 = new Intent(MainActivity.this, our_doctrinal_statement.class);
                    startActivity(intent3);
                    break;
                case R.id.nav_omp:
                    Intent intent4 = new Intent(MainActivity.this, our_musical_philosophy.class);
                    startActivity(intent4);
                    break;
                case R.id.nav_share:
                    Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    sendIntent.setType("text/plain");
                    sendIntent.putExtra(Intent.EXTRA_TEXT, "Hey, download this app!");
                    startActivity(sendIntent);
                    break;
            }
            return false;
        }
    });

    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new HomeFragment()).commit();
    }

    Toolbar toolbar1 = findViewById(R.id.toolbar1);
    setSupportActionBar(toolbar1);

    drawerLayout = findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, toolbar1, R.string.navigation_drawer_open,  R.string.navigation_drawer_close);
    drawerLayout.setDrawerListener(drawerToggle);
    drawerToggle.syncState();
    drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawerLayout.openDrawer(GravityCompat.START);
        }
    });
}

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item))
        return true;
    int id = item.getItemId();
        if (id == R.string.app_name) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    if(drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }
    else {
        super.onBackPressed();
    }
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected( MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_home:
                        selectedFragment = new HomeFragment();
                        break;
                    case R.id.nav_favorites:
                        selectedFragment = new FavoritesFragment();
                        break;
                    case R.id.nav_category:
                        selectedFragment = new CategoryFragment();
                        break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        selectedFragment).commit();
                return true;
            }
        };
}

0 个答案:

没有答案