ящик навигации не отображается должным образом - PullRequest
0 голосов
/ 03 апреля 2019

Я занимаюсь разработкой новостного приложения и установил навигационный ящик используя следующую ссылку , но когда я запускаю приложение кода, показывающее пустой белый экран.

ниже моего класса MainActivity.java

 public class MainActivity extends AppCompatActivity {
         private DrawerLayout mDrawer;
         private Toolbar toolbar;


         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);
              NavigationView 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
             @SuppressLint("ResourceType") ImageView ivHeaderPhoto = headerLayout.findViewById(R.drawable.ic_sportnews);
             setupDrawerContent(nvDrawer);

         }

         @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();
         }


     }

ниже моего 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="wrap_content"
        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>

ниже моего класса фрагмента

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;


    }
}

screenshot of empty screen

...