底部导航视图 - 替换碎片非常慢

时间:2017-08-29 04:58:51

标签: android performance android-fragments bottomnavigationview

我有一个带有底部导航视图的设置。我加载的3个片段之一有一个标签栏,另有3个片段。 我在底部导航点击中替换这样的片段:

    private void loadPage(int page)
    {

    if(fragmentManager == null)
        fragmentManager = getSupportFragmentManager();
    final FragmentTransaction transaction = fragmentManager.beginTransaction();
    //transaction.setCustomAnimations(R.anim.fragment_fade_in, R.anim.fragment_fade_out, R.anim.fragment_fade_in, R.anim.fragment_fade_out);
    //transaction.setCustomAnimations(R.anim.fragment_fade_in, R.anim.fragment_fade_out, R.anim.fragment_fade_in, R.anim.fragment_fade_out);

    switch (page)
    {
        //Property
        case TAB_PROPERTIES:

            Fragment fragment = fragmentManager.findFragmentByTag("property");
            if(fragment == null)
                fragment = PropertyFragment.newInstance();
            transaction.replace(R.id.rlMainContent, fragment,"property").addToBackStack("property").commit();
            break;
        //jobs
        case TAB_JOBS:
            fragment = fragmentManager.findFragmentByTag("jobs");
            if(fragment == null)
                fragment = JobsFragment.newInstance(null);
            transaction.replace(R.id.rlMainContent, fragment,"jobs").addToBackStack("jobs").commit();
            break;
        //contacts
        case TAB_CONTACTS:
            fragment = fragmentManager.findFragmentByTag("contacts");
            if(fragment == null)
                fragment = PersonalContactsFragment.newInstance(false,true,false,true);

            transaction.replace(R.id.rlMainContent, fragment,"contacts").addToBackStack("contacts").commit();

            break;
    }



}

正确地从后栈中检索片段,然后调用onCreateView和onStart。

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.activities_fragment, container, false);
    mEmptyView = view.findViewById(R.id.empty_view);
    mRecyclerView = (EmptyRecyclerView) view.findViewById(R.id.recycler_view);
    mSwipeContainer = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer_OwnerRequest);
    mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar3);
    if (!mFirstStartup)
        mProgressBar.setVisibility(View.GONE);


    return view;
}


    @Override
public void onStart()
{
    super.onStart();

    if(mRecyclerView.getAdapter() == null)
    {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mRecyclerAdapter);
        mRecyclerView.addItemDecoration(mHeadersDecor);
        mSwipeContainer.setOnRefreshListener(Refresh);
        mSwipeContainer.setColorSchemeResources(R.color.colorPrimary, R.color.white);


    }


}

加载缓慢的片段会在pagerAdapter中创建上面的3个片段。不调用setAdapter()会使它流畅,但当然不会渲染任何东西。我的recyclerview的布局是一个非常扁平的布局构建,带有ConstraintsLayout。

有没有人知道如何处理这样的情况?

之前我使用的是pagerAdapter并缓存了bottomNavView的所有3个片段,但我认为这是不好的做法,因为没有滑动手势,也没有必要加载和缓存所有片段。

0 个答案:

没有答案