导航抽屉按钮无响应。

huangapple 未分类评论58阅读模式
英文:

Navigation drawer buttons are not working

问题

我遇到了一个导航抽屉的问题,出现了一些按钮无法正常工作的情况。每当我点击任何按钮时,导航抽屉都会简单地关闭。以下是我的代码,请帮忙看看。例如,当我点击与 nav.homee 相关的按钮时,不会创建任何提示,抽屉只是关闭了。

DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);

mAppBarConfiguration = new AppBarConfiguration.Builder(
        R.id.nav_homee, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_home)
        .setDrawerLayout(drawer)
        .build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

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

    int id = item.getItemId();
    if (id == R.id.nav_home) {

    } else if (id == R.id.nav_gallery) {
        String link = "Check out the best taxi app in Zed! https://play.google.com/store/apps/details?id=com.chomba.haroldking.uta";
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, link);
        startActivity(Intent.createChooser(share, "UTA share"));
        return true;
    } else if (id == R.id.nav_homee) {
        Toast toast = Toast.makeText(getApplicationContext(), "UTA standard is still under", Toast.LENGTH_LONG);
        View view1 = toast.getView();
        view1.setBackgroundResource(R.color.mdtp_transparent_black);
        TextView text = (TextView) view1.findViewById(android.R.id.message);
        text.setTextColor(Color.parseColor("#CDEAF7"));
        toast.show();
        return true;
    } else if (id == R.id.nav_slideshow) {
        Intent intent1 = new Intent(Main14Activity.this, Main7Activity.class);
        startActivity(intent1);
        return true;
    }
    DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

以下是我的 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">
 <include
    layout="@layout/app_bar_main14"
    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_main14"
    app:menu="@menu/activity_main14_drawer" />

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

如果您需要更多的帮助,请随时提问。

英文:

I am experiencing a problem with my navigation drawer, for some reason the buttons are not working. Every time I click on any button the navigation drawer simply closes. This is my code below, please help. An example is when I click on the button associated with nav.homee No toast is created and the drawer simply closes

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_homee, R.id.nav_gallery, R.id.nav_slideshow,R.id.nav_home)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main14, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
    int id=item.getItemId();

    if (id==R.id.action_settings){
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@SuppressWarnings(&quot;StatementWithEmptyBody&quot;)
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item){
    

    int id=item.getItemId();
    if (id==R.id.nav_home){

    }else if (id==R.id.nav_gallery){
        String link=&quot;Check out the best taxi app in Zed! https://play.google.com/store/apps/details?id=com.chomba.haroldking.uta&quot;;
        Intent share=new Intent(Intent.ACTION_SEND);
        share.setType(&quot;text/plain&quot;);
        share.putExtra(Intent.EXTRA_TEXT, link);
        startActivity(Intent.createChooser(share,&quot;UTA share&quot;));
        return true;
    }else if (id==R.id.nav_homee){
        Toast toast=Toast.makeText(getApplicationContext(),&quot;UTA standard is still under&quot;,Toast.LENGTH_LONG);
        View view1=toast.getView();
        view1.setBackgroundResource(R.color.mdtp_transparent_black);
        TextView text=(TextView)view1.findViewById(android.R.id.message);
        text.setTextColor(Color.parseColor(&quot;#CDEAF7&quot;));
        toast.show();
        return true;
       // Intent intent1 = new Intent(Main14Activity.this, Standarduta.class);
        //startActivity(intent1);
    }else if (id==R.id.nav_slideshow){
        Intent intent1 = new Intent(Main14Activity.this, Main7Activity.class);
        startActivity(intent1);
        return true;
    }
    DrawerLayout drawerLayout=findViewById(R.id.drawer_layout);
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

}
Below is my xml

      &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;android.support.v4.widget.DrawerLayout  xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/drawer_layout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:fitsSystemWindows=&quot;true&quot;
tools:openDrawer=&quot;start&quot;&gt; 
 &lt;include
    layout=&quot;@layout/app_bar_main14&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot; /&gt;

   &lt;android.support.design.widget.NavigationView
    android:id=&quot;@+id/nav_view&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;match_parent&quot;
    android:layout_gravity=&quot;start&quot;
    android:fitsSystemWindows=&quot;true&quot;
    app:headerLayout=&quot;@layout/nav_header_main14&quot;
    app:menu=&quot;@menu/activity_main14_drawer&quot; /&gt;

  &lt;/android.support.v4.widget.DrawerLayout&gt;

huangapple
  • 本文由 发表于 2020年4月11日 01:54:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/61145886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定