导航抽屉未正确显示

2024-02-21

我正在开发新闻应用程序,并且已经实现了导航抽屉 使用以下link https://guides.codepath.com/android/fragment-navigation-drawer但是当我运行代码应用程序时显示空白色 屏幕。

在我的 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>

在我的 Fragment 类下面

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;


    }
}

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

导航抽屉未正确显示 的相关文章

随机推荐

  • sudo: python: 找不到命令

    我要实现sudo python找到Python 3 我遇到了一个奇怪的问题 在终端中输入python version给了 3 6 但是sudo python version给了2 7 经过尝试一些事情后我终于卸载了 2 7sudo apt
  • jQuery 用于获取表行上的 Click 事件

    我有下表 table tr class rows td cell1 td td cell2 td tr table 如果我点击了 如何设置警报消息any of the column of 使用jquery 您可以使用委托来获得更好的性能 它
  • JavaScript 中这个东西是什么?

    考虑 var something wtf null omg null 自从我上次使用 JavaScript 编程以来 我的 JavaScript 知识仍然非常零碎 但我想我现在已经重新学习了大部分知识 除了这个 我不记得以前见过这个 它是什
  • 如何映射对象的键以使 JSON 更容易在 React 中处理

    如果我有这个 JSON name Active user config status active name Paused user config status active 然后我可以渲染一个 React 组件并轻松访问数据 render
  • jenkins pipelines:shell脚本无法获取更新的环境变量

    在 Jenkins 中 我想获取用户输入并传递给 shell 脚本以供进一步使用 我尝试设置为环境变量 但shell脚本无法获取最新值 旧值是echo pipeline agent none environment myVar someth
  • Rails 中按月和年对记录进行分组

    我使用的是 Ruby 1 9 2 Rails 3 0 x 并且使用 MySQL DB 我有一个消息模型 每天都可以发布新消息 我有一个索引操作 我想在其中显示按月份和年份分组的这些消息 这主要用于过滤消息 我的查询如下所示 active m
  • 我实际上如何从方法外部获取异步 Web 请求的响应?

    我有点困惑 我正在尝试以异步方式发布到我的网络服务 理想情况下我想启动请求 在 UI 上显示加载微调器 然后当异步请求完成时处理响应 并且如果有错误则显示错误 或对结果进行其他操作 这是我的代码 我在这里调用请求并传递一些数据 privat
  • md5 目录树中的所有文件

    我有一个结构如下的目录 Test txt Test1 Test1 txt Test1 copy txt Test1a Test1a txt Test1a copy txt Test2 Test2 txt Test2 copy txt Tes
  • 如何在qsub中指定错误日志文件和输出文件

    我有一个 qsub 脚本 submit job sh bin sh N job1 t 1 100 cwd SEEDFILE home user1 data1 SEED sed n e SGE TASK ID p SEEDFILE home
  • 如何在 CloudFormation 中指定签名的 S3 URL 作为模板?

    在 AWS CloudFormation 中 您可以通过上传模板文件或指定模板的 S3 URL 来指定模板 指定 Amazon S3 模板 URL 如果存储桶是公共的 您可以构造一个 URL 供任何人访问该对象 模板 只要 S3 模板 UR
  • Ellipsize 属性在 Android 4.0 上不起作用

    我目前正在 Android 4 0 Ice Cream Sandwich 上测试一个应用程序 然后再向市场发布更新 在测试过程中 我意识到椭圆形属性停止工作 我在列表视图上使用它来截断太长的项目标题 在 Android 2 3 7 上 一切
  • oracle中的子选择

    我正在尝试从子选择中的另一个表中选择最新价格 但我不知道如何让它发挥作用 这是我尝试过的 select something somthingelse select from select QUOTE PRICE as old price f
  • Android Firebase:无法解析

    我尝试将 Firebase 与我的应用程序连接 但出现了此错误 这是我的 build gradle 项目 Top level build file where you can add configuration options common
  • createReadStream() 抛出 RangeError:上传文件时超出最大调用堆栈大小

    我正在尝试使用 Apollo 服务器Upload直接将文件发送到 S3 的标量 我的架构 const gql require apollo server express module exports gql extend type Muta
  • Python:使用“copyreg”为已有减速器的类型定义减速器

    请记住 我正在使用 Python 3 工作 因此解决方案需要在 Python 3 中工作 我想使用copyreg教 Python 如何 pickle 函数的模块 当我尝试这样做时 Pickler对象仍然会尝试使用 pickle 函数save
  • NSURLSessionConfiguration 和 NSURLRequest 具有相同的属性吗?

    我在官方文档中看到 NSURLSessionConfiguration 和 NSURLRequest 共享一些相同或相似的属性 比如NSURLRequestCachePolicy networkServiceType timeOurInte
  • 在具有属性的两个表单之间传递数据[重复]

    这个问题在这里已经有答案了 我正在 C 中的 2 个窗口窗体之间传递数据 Form1 是主窗体 其文本框将接收从 form2 textbox 传递给它的文本并将其显示在其文本框 form1 textbox 中 首先 form1 打开 带有一
  • React 测试库 - 在 fireEvent 之后使用“await wait()”

    我正在尝试使用测试库在 fireEvent click 之后检查 DOM 元素 我知道我需要在 fireEvent 之后等待 但不确定为什么简单地使用 wait 不起作用 下面是以两种方式编写的相同测试 第一个失败 第二个通过 我不明白为什
  • .htaccess 将index.php重定向到/

    我想隐藏index php页面并只显示域 htaccess 可以吗 RewriteRule index php L R 301 NC 还尝试过 RewriteEngine On RewriteBase RewriteCond THE REQ
  • 导航抽屉未正确显示

    我正在开发新闻应用程序 并且已经实现了导航抽屉 使用以下link https guides codepath com android fragment navigation drawer但是当我运行代码应用程序时显示空白色 屏幕 在我的 M