无法获取导航抽屉项目以正确显示项目
Posted
技术标签:
【中文标题】无法获取导航抽屉项目以正确显示项目【英文标题】:Unable to Get Navigation Drawer Items to Display Items Correctly 【发布时间】:2018-07-16 21:38:43 【问题描述】:我在一个空白项目中有一个可用的导航抽屉,但是每当我尝试在现有项目中实现导航抽屉时,它似乎无法正常工作。
The navigation drawer displays fine, however, when items inside are selected, it doesn't seem to redirect to the fragment selected.
我尝试了很多资源,但是我仍然无法进步。
提前致谢。
下面是我的代码:
NavigationDrawer 类
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
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.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
public class NDrawer extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
About.OnFragmentInteractionListener, Home.OnFragmentInteractionListener
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Fragment fragment;
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
@Override
public void onBackPressed()
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START))
drawer.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.main, 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)
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment;
if (id == R.id.nav_home)
fragment = new Home();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.detach(fragment);
ft.attach(fragment);
ft.commit();
else if (id == R.id.nav_rooms)
else if (id == R.id.nav_account)
else if (id == R.id.nav_settings)
else if (id == R.id.nav_help)
else if (id == R.id.nav_about)
fragment = new About();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.detach(fragment);
ft.attach(fragment);
ft.commit();
else if (id == R.id.nav_signout)
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
@Override
public void onFragmentInteraction(Uri uri)
关于片段类
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple @link Fragment subclass.
* Activities that contain this fragment must implement the
* @link About.OnFragmentInteractionListener interface
* to handle interaction events.
* Use the @link About#newInstance factory method to
* create an instance of this fragment.
*/
public class About extends Fragment
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public About()
// Required empty public constructor
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment About.
*/
public static About newInstance(String param1, String param2)
About fragment = new About();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
if (getArguments() != null)
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_about, container, false);
public void onButtonPressed(Uri uri)
if (mListener != null)
mListener.onFragmentInteraction(uri);
@Override
public void onAttach(Context context)
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener)
mListener = (OnFragmentInteractionListener) context;
else
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
@Override
public void onDetach()
super.onDetach();
mListener = null;
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener
void onFragmentInteraction(Uri uri);
activity_main.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_
android:layout_
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".UserActivity">
<TextView
style="@style/Base.TextAppearance.AppCompat.Headline"
android:id="@+id/displayUsername"
android:layout_
android:layout_
android:padding="5dp"
android:layout_margin="5dp"/>
<Button
android:id="@+id/listen_button"
android:layout_
android:layout_
android:text="Listen"
android:onClick="buttonClicked"
/>
<TextView
android:layout_
android:layout_
android:text=""
android:id="@+id/textView"
/>
<TextView
android:id="@+id/status"
android:layout_
android:layout_
android:text="" />
<TextView
style="@style/Base.TextAppearance.AppCompat.Headline"
android:layout_
android:layout_
android:layout_gravity="center_horizontal"
android:text="@string/signed_in_header" />
<Button android:id="@+id/btn_settings"
android:text="@string/settings"
android:layout_marginTop="40dp"
android:layout_
android:layout_
android:onClick="buttonClicked" />
<Button android:id="@+id/btn_sign_out"
android:text="@string/sign_out"
android:layout_marginTop="40dp"
android:layout_
android:layout_
android:onClick="buttonClicked" />
<TextView
android:id="@+id/displayUsername2"
android:layout_
android:layout_
android:padding="5dp"
android:layout_margin="5dp"/>
</LinearLayout>
<include
layout="@layout/app_bar_nav"
android:layout_
android:layout_ />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_nav"
app:menu="@menu/activity_nav_drawer" />
</android.support.v4.widget.DrawerLayout>
activity_nav_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
+6 <group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:checked="true"
android:title="Home" />
<item
android:id="@+id/nav_rooms"
android:icon="@drawable/ic_room_black"
android:title="All Rooms" />
<item
android:id="@+id/nav_account"
android:icon="@drawable/ic_account_box_black"
android:title="Account" />
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings_black"
android:title="Settings" />
<item
android:id="@+id/nav_help"
android:icon="@drawable/ic_help_black"
android:title="Help" />
<item
android:id="@+id/nav_about"
android:title="About" />
<item
android:id="@+id/nav_signout"
android:icon="@drawable/ic_input_black"
android:title="Logout" />
</group>
</menu>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".NDrawer"
tools:showIn="@layout/app_bar_nav">
<TextView
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_
android:layout_
android:id="@+id/content_frame"
android:orientation="horizontal" />
</android.support.constraint.ConstraintLayout>
app_bar_nav.xml
<?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_
android:layout_
tools:context="aviraj.firebaseapp.NDrawer">
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
【问题讨论】:
【参考方案1】:但老实说 - 再试一次,从模板类从头开始实现抽屉导航类,并逐个方法地放入我们的其他主要类,以查看任何可能的冲突。
【讨论】:
以上是关于无法获取导航抽屉项目以正确显示项目的主要内容,如果未能解决你的问题,请参考以下文章