Android:如何将片段加载到FrameLayout中 [英] Android: how to load fragment into FrameLayout

查看:278
本文介绍了Android:如何将片段加载到FrameLayout中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Android开始,在我的项目中,我正在使用 BottomBar .喜欢它,效果很好.我的应用程序代码与他在本教程中使用的代码几乎相同,唯一不同的是我的MainActivityAppCompatActivity扩展.

I'm starting with Android and in my project I'm using this BottomBar. Love it, works pretty well. The code of my application is almost identical on to the one he uses in the tutorial, the only different thing is that my MainActivity extends from AppCompatActivity.

现在我要做的是在FrameLayout中加载片段:

Now what I'm trying to do is to load fragments in the FrameLayout:

<!-- This could be your fragment container, or something -->
<FrameLayout
    android:id="@+id/contentContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottomBar"
    />

为此,我正在尝试这段代码,我在谷歌上搜索了我的问题的答案:

To do so I'm trying this piece of code, which I found googling for the answer of my issue:

// Create new fragment and transaction
QrCodeFragment newFragment = new QrCodeFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.bottomBar, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

transaction.replace(R.id.bottomBar, newFragment);行抱怨newFragment的类型,应该是android.app.Fragment.我的QrCode类:public class QrCodeFragment extends Fragment

The line transaction.replace(R.id.bottomBar, newFragment); is complaining about the type of newFragment, which should be android.app.Fragment. My QrCode class: public class QrCodeFragment extends Fragment

当我今天从Android开始时,如果我做对了,我会感到困惑. 如果我真的应该在FrameLayout中加载片段,如果是的话,我做错了我无法加载的片段.我知道这是片段的类型,但是我不知道如何用所需的类型创建它.我使用Android Studio > New > Fragment > Fragment (Blank)

As I'm starting with Android today I'm confused if I'm doing the right thing. If I should really load fragments inside the FrameLayout and if so, what am I doing wrong that I can't load it. I know it's the type of my fragment, but I don't know how to create it with the required type. I created it using Android Studio > New > Fragment > Fragment (Blank)

感谢您的帮助

更新: 我在此文章中找到了解决错误的方法,但即使没有错误,我仍然看不到该片段.

UPDATE: I found the solution to my error in this post but even not having the error I still can't see the fragment.

推荐答案

首先,您在Fragment交易行中犯了一个错误,根据您的布局应该是:

First you have a mistake in your Fragment transaction line, according with your layout should be:

transaction.replace(R.id.contentContainer, newFragment); // not R.id.bottomBar

第二,您应该使用 supportFragmentManager 而不是 fragmentManager 来处理支持片段,因此请实施以下方式:

Second, you should use supportFragmentManager instead of fragmentManager to work with support fragments, so implement the following way:

final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.contentContainer, newFragment);
transaction.addToBackStack(null);
transaction.commit();

这篇关于Android:如何将片段加载到FrameLayout中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆