【问题标题】:Android: how to load fragment into FrameLayoutAndroid:如何将片段加载到 FrameLayout
【发布时间】:2017-05-31 18:58:02
【问题描述】:

我从 Android 开始,在我的项目中我使用的是 BottomBar。很喜欢,效果不错。我的应用程序代码与他在教程中使用的代码几乎相同,唯一不同的是我的MainActivity 扩展自AppCompatActivity

现在我要做的是在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"
    />

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

// 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。我的二维码课:public class QrCodeFragment extends Fragment

当我今天开始使用 Android 时,我很困惑我是否做对了。 如果我真的应该在FrameLayout 中加载片段,如果是这样,我做错了什么,我无法加载它。我知道这是我的片段的类型,但我不知道如何使用所需的类型创建它。我使用Android Studio &gt; New &gt; Fragment &gt; Fragment (Blank)创建它

感谢您的帮助


更新: 我在this post 中找到了我的错误的解决方案,但即使没有错误我仍然看不到片段。

【问题讨论】:

  • 据我了解,容器就是你将片段充气到的东西。这意味着如果你将它膨胀成 FrameLayout,它应该仍然没问题 - 试试看。如果“complain”表示编译错误或crash,你应该指定
  • @LunarWatcher 我实际上发现了这个问题,它不再让我出错了。我应该只导入 android.app.Fragment;而不是导入 android.support.v4.app.Fragment; .现在的问题是它没有显示我刚刚设置显示的片段

标签: java android android-fragments


【解决方案1】:

首先你的 Fragment 交易行有错误,根据你的布局应该是:

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

其次,您应该使用 supportFragmentManager 而不是 fragmentManager 来处理支持片段,因此实现以下方式:

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

【讨论】:

  • 感谢您的回答,它解决了这个问题。实际上,我只需要输入正确的 id (contentContainer)。
【解决方案2】:

在我的情况下,我通过使用来解决它

androidx.fragment.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

更换你的

片段交易

【讨论】:

    【解决方案3】:
                bottomNavigationView= findViewById(R.id.bottom_navigation);
               bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    
    
    
                   @SuppressLint("NonConstantResourceId")
                   @Override
                   public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                       Fragment temp =null;
                       switch (item.getItemId()) {
                           case R.id.library:
                                temp = new LibraryFragment();
    
                               
                               break;
                           case R.id.search:
                               temp = new SearchFragment();
                               break;
                           case R.id.profile:
                               temp = new UserprofileFragment();
                               break;
                           default:
                       }
                      
    
                   getSupportFragmentManager().beginTransaction().replace(R.id.fragment, temp).commit();
    
    
                            return true;
                   }
               });
    
    
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment,new SearchFragment()).commit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      相关资源
      最近更新 更多