在底栏上保持碎片活着

时间:2016-05-12 06:54:01

标签: android android-fragments

我正在尝试使用BottomBar在Android上实现新的设计模式,而我正在使用一个库。

有没有办法让片段保持活着(也许像ViewPager一样?)所以当用户更改Tabs并转到他已经访问过的片段时,我不必再次重新创建片段?

1 个答案:

答案 0 :(得分:1)

这就是我所做的。它并不完美,可以进一步改进。我已经包含了关于什么做的评论。

    public class MainHolderFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";

    FragmentMainHolderBinding mBinder;
    FragmentManager mFragmentManager;
    Fragment mFragment;
    Fragment homeFragment = HomeFragment.newInstance();
    Fragment searchFragment = SearchFragment.newInstance();
    Fragment addFragment = AddFragment.newInstance();
    Fragment activityFragment = ActivityFragment.newInstance();
    Fragment profileFragment = ProfileFragment.newInstance();

    public MainHolderFragment() {

    }

    public static MainHolderFragment newInstance() {
        MainHolderFragment fragment = new MainHolderFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinder = DataBindingUtil.inflate(inflater, R.layout.fragment_main_holder, container, false);
        mFragmentManager = getChildFragmentManager();
        createFragments();
        adjustTabs();
        setUIListeners();
        return mBinder.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private void setUIListeners() {
        mBinder.bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                switch (tabId) {
                    case R.id.tab_home:
                        hideShowFragment(mFragment, homeFragment);
                        mFragment = homeFragment;
                        break;
                    case R.id.tab_search:
                        hideShowFragment(mFragment, searchFragment);
                        mFragment = searchFragment;
                        break;
                    case R.id.tab_add:
                        hideShowFragment(mFragment, addFragment);
                        mFragment = addFragment;
                        break;
                    case R.id.tab_activity:
                        hideShowFragment(mFragment, activityFragment);
                        mFragment = activityFragment;
                        break;
                    case R.id.tab_profile:
                        hideShowFragment(mFragment, profileFragment);
                        mFragment = profileFragment;
                        break;
                }
            }
        });
    }

    //Method to add and hide all of the fragments you need to. In my case I hide 4 fragments, while 1 is visible, that is the first one.
    private void addHideFragment(Fragment fragment) {
        mFragmentManager.beginTransaction().add(R.id.frame_container, fragment).hide(fragment).commit();
    }

    //Method to hide and show the fragment you need. It is called in the BottomBar click listener.
    private void hideShowFragment(Fragment hide, Fragment show) {
        mFragmentManager.beginTransaction().hide(hide).show(show).commit();
    }

    //Add all the fragments that need to be added and hidden. Also, add the one that is supposed to be the starting one, that one is not hidden.
    private void createFragments() {
        addHideFragment(searchFragment);
        addHideFragment(addFragment);
        addHideFragment(activityFragment);
        addHideFragment(profileFragment);
        mFragmentManager.beginTransaction().add(R.id.frame_container, homeFragment).commit();
        mFragment = homeFragment;
    }

    //I wanted to remove the titles and lower the icons a little bit.
    private void adjustTabs() {
        mBinder.bottomBar.getTabWithId(R.id.tab_home).setPadding(0, 30, 0, 0);
        mBinder.bottomBar.getTabWithId(R.id.tab_search).setPadding(0, 30, 0, 0);
        mBinder.bottomBar.getTabWithId(R.id.tab_add).setPadding(0, 30, 0, 0);
        mBinder.bottomBar.getTabWithId(R.id.tab_activity).setPadding(0, 30, 0, 0);
        mBinder.bottomBar.getTabWithId(R.id.tab_profile).setPadding(0, 30, 0, 0);
    }
}