【发布时间】:2019-04-03 12:47:35
【问题描述】:
您好,我正在使用导航抽屉开发新闻应用程序,但它没有显示仅显示一个片段的导航,我关注了这个tutorial
在 MainActivity.java 文件下方
public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawer; private Toolbar toolbar; private NavigationView nvDrawer; private ActionBarDrawerToggle drawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Set a Toolbar to replace the ActionBar. toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Find our drawer view mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawerToggle = setupDrawerToggle(); // Tie DrawerLayout events to the ActionBarToggle mDrawer.addDrawerListener(drawerToggle); nvDrawer = (NavigationView) findViewById(R.id.nvView); // Inflate the header view at runtime View headerLayout = nvDrawer.inflateHeaderView(R.layout.nav_header); // We can now look up items within the header if needed ImageView ivHeaderPhoto = (ImageView) headerLayout.findViewById((R.id.header_image)); setupDrawerContent(nvDrawer); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.flContent, new BBCSportFragment()).commit(); fragmentManager.beginTransaction().replace(R.id.flContent, new FoxSportsFragment()).commit(); fragmentManager.beginTransaction().replace(R.id.flContent, new TalkSportsFragment()).commit(); } @Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. switch (item.getItemId()) { case android.R.id.home: mDrawer.openDrawer(GravityCompat.START); return true; } return super.onOptionsItemSelected(item); } private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener( menuItem -> { selectDrawerItem(menuItem); return true; }); } public void selectDrawerItem(MenuItem menuItem) { // Create a new fragment and specify the fragment to show based on nav item clicked Fragment fragment = null; Class fragmentClass = null; switch (menuItem.getItemId()) { case R.id.bbcsports_fragment: fragmentClass = BBCSportFragment.class; break; case R.id.talksports_fragment: fragmentClass = TalkSportsFragment.class; break; case R.id.foxsports_fragment: fragmentClass = FoxSportsFragment.class; break; default: } try { fragment = (Fragment) fragmentClass.newInstance(); } catch (Exception e) { e.printStackTrace(); } // Insert the fragment by replacing any existing fragment FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit(); // Highlight the selected item has been done by NavigationView menuItem.setChecked(true); // Set action bar title setTitle(menuItem.getTitle()); // Close the navigation drawer mDrawer.closeDrawers(); } private ActionBarDrawerToggle setupDrawerToggle() { // NOTE: Make sure you pass in a valid toolbar reference. ActionBarDrawToggle() does not require it // and will not render the hamburger icon without it. return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close); } }
在activity_main.xml下面我实现了抽屉导航和抽屉布局。
<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/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- This LinearLayout represents the contents of the screen --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- The ActionBar displayed at the top --> <include layout="@layout/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!-- The main content view where fragments are loaded --> <FrameLayout android:id="@+id/flContent" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </LinearLayout> <!-- The navigation drawer that comes from the left --> <!-- Note that `android:layout_gravity` needs to be set to 'start' --> <android.support.design.widget.NavigationView android:id="@+id/nvView" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/white" app:headerLayout="@layout/nav_header" app:menu="@menu/navigation_menu" /> </android.support.v4.widget.DrawerLayout>
在 BBCFragment.java 下
public class BBCSportFragment extends Fragment { public List<Article> articleList = new ArrayList<Article>(); @BindView(R.id.recycler_view) RecyclerView recyclerView; private SportNews sportNews; private ArticleAdapter articleAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_bbcsport, container, false); ButterKnife.bind(this, view); SportInterface sportInterface = SportClient.getApiService(); Call<SportNews> call = sportInterface.getArticles(); call.enqueue(new Callback<SportNews>() { @Override public void onResponse(Call<SportNews> call, Response<SportNews> response) { sportNews = response.body(); if (sportNews != null && sportNews.getArticles() != null) { articleList.addAll(sportNews.getArticles()); } articleAdapter = new ArticleAdapter(articleList, sportNews); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext()); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(articleAdapter); } @Override public void onFailure(Call<SportNews> call, Throwable t) { } }); return view; } }
在 bbc_fragment.xml 下方
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- The main content view --> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Put what you want as your normal screen in here, you can also choose for a linear layout or any other layout, whatever you更喜欢 -->
【问题讨论】:
-
尝试将navigationview作为第一个孩子,然后将linearlayout作为内容
标签: java android navigation navigation-drawer