简体   繁体   English

如何在 AppCompatActivity.onCreate() 中增加片段的布局?

[英]How to inflate fragment's layout in AppCompatActivity.onCreate()?

Im creating a simple app to learn fragments.我正在创建一个简单的应用程序来学习片段。 It consists of a bottomNavigationView with three menu items, and a FrameLayout thats serves as a containter for fragments and takes up the rest of the space.它由一个包含三个菜单项的 bottomNavigationView 和一个用作片段容器并占据其余空间的 FrameLayout 组成。 Selecting an item switches the Fragment in FrameLayout with:选择一个项目将 FrameLayout 中的 Fragment 切换为:

getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer,selectedFragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();

When i ran the app, i noticed that transitions between fragments are very slow.当我运行该应用程序时,我注意到片段之间的转换非常缓慢。 Profiler revealed that it was due to inflation of the layout each time. Profiler 透露,这是由于每次布局膨胀所致。 So i modified onCreateView() method of fragments to inflate just once and then return already inflated layout.所以我修改了片段的 onCreateView() 方法只膨胀一次,然后返回已经膨胀的布局。

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(ui == null)
            inflateLayout(inflater, container);
        return ui;

    }

This fixed the issue partialy because first transition was still slow.这部分地解决了这个问题,因为第一次转换仍然很慢。 I tried to inflate fragment UI in activity, but that resulted in an error: Fragment isn't attached to Context.我试图在活动中膨胀片段 UI,但这导致错误:片段未附加到上下文。 So i just switched between all the fragments in onCreate() of main activity:所以我只是在主要活动的 onCreate() 中的所有片段之间切换:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView nav = findViewById(R.id.bottom_nav);
    homeFragment = new HomeFragment();
    favFragment = new FavFragment();
    starFragment = new StarFragment();
    nav.setSelectedItemId(R.id.home);
    getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, starFragment).commit();
    getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, favFragment).commit();
    getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, homeFragment).commit();
    nav.setOnNavigationItemSelectedListener(listener);
}

That made things better, but recyclerView which is contained within StarFragment still creates viewHolders after the "real" transaction (initiated by listener), whitch takes 20ms for a single viewHolder (i'll look into why later).这让事情变得更好,但是包含在 StarFragment 中的 recyclerView 仍然在“真实”事务(由侦听器启动)之后创建 viewHolders,单个 viewHolder 需要 20 毫秒(稍后我将研究原因)。

My question is: how to make those transactions between fragments quick?我的问题是:如何在片段之间快速进行这些交易? Is what im trying to do right, or is there a better way?我正在尝试做的事情是正确的,还是有更好的方法?

I am not sure how complex your layouts are, however, loading UI elements in the layout of a fragment should not take much time if the data loaders (reading values for your RecyclerView from the database, network calls) are off-threaded.我不确定您的布局有多复杂,但是,如果数据加载器(从数据库中读取RecyclerView值、网络调用)是离线的,则在片段布局中加载 UI 元素应该不会花费太多时间。

You really do not have to do all the fragment transactions in the onCreate function of your activity.您确实不必在活动的onCreate函数中执行所有片段事务。 Just load the fragments (or do the fragment transaction) on demand when you click an item in the bottom navigation bar.当您单击底部导航栏中的项目时,只需按需加载片段(或执行片段事务)。 You are creating instances of the fragment in your onCreate function which is perfectly fine and in that way, each time you are switching among fragments are not creating new instances of your fragment class.您正在onCreate函数中创建片段的实例,这非常好,这样,每次在片段之间切换时,都不会创建片段类的新实例。

I am not sure if the initial loading your fragments can be more optimized, however, you may consider having a ViewPager so that the fragments are loaded all the time after you are done with their first time loading.我不确定是否可以对片段的初始加载进行更优化,但是,您可以考虑使用ViewPager以便在第一次加载完成后始终加载片段。

Here's a good tutorial on how you can add a ViewPager with a bottom navigation bar. 这是一个关于如何添加带有底部导航栏的ViewPager 的好教程 I hope that helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM