java BottomBarNavigation代码保留片段状态

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java BottomBarNavigation代码保留片段状态相关的知识,希望对你有一定的参考价值。

package com.ouida.app.ui.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.ouida.app.R;
import com.ouida.app.ui.base.BaseActivity;
import com.ouida.app.ui.book_club.BookClubFragment;
import com.ouida.app.ui.my.MyBooksFragment;
import com.ouida.app.ui.profile.ProfileFragment;
import com.ouida.app.ui.rewards.RewardsFragment;
import com.ouida.app.ui.store.StoreFragment;
import com.ouida.app.utils.others.BottomNavigationViewBehavior;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DashboardActivity extends BaseActivity implements DashboardMvpView,
        BottomNavigationView.OnNavigationItemSelectedListener {

    public static final String SELECTED_ITEM = "arg_selected_item";
    private static String MY_BOOKS = "my books";
    private static String PROFILE = "profile";
    private static String BOOK_CLUB = "book club";
    private static String STORE = "store";
    private static String REWARDS = "rewards";

    Fragment myBooksFragment;
    Fragment storeFragment;
    Fragment bookClubFragment;
    Fragment rewardsFragment;
    Fragment profileFragment;

    @BindView(R.id.navigation)
    BottomNavigationView mBottomNavigationView;
    @BindView(R.id.container)
    FrameLayout frameLayout;
    @Inject
    DashboardMvpContract<DashboardMvpView> mPresenter;
    FragmentManager mFragmentManager = getSupportFragmentManager();
    Fragment mFragment;
    private int mSelectedItem = 0;

    @NonNull
    public static Intent getStartIntent(Context context) {
        return new Intent(context, DashboardActivity.class);
    }

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

        setContentView(R.layout.activity_dashboard);

        getActivityComponent().inject(this);

        setUnBinder(ButterKnife.bind(this));

        mPresenter.onAttach(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            default:
                break;
        }
        return false;
    }

    @Override
    protected void setUp() {
        mPresenter.onViewInitialized();

        myBooksFragment = MyBooksFragment.newInstance();
        storeFragment = StoreFragment.newInstance();
        bookClubFragment = BookClubFragment.newInstance();
        rewardsFragment = RewardsFragment.newInstance();
        profileFragment = ProfileFragment.newInstance();

        createFragments();

        mBottomNavigationView.setOnNavigationItemSelectedListener(this);

        mSelectedItem = getIntent().getIntExtra(SELECTED_ITEM, 0);

        MenuItem homeItem = mBottomNavigationView.getMenu().getItem(mSelectedItem);
        selectFragment(homeItem);
        mBottomNavigationView.setSelectedItemId(mSelectedItem);

        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mBottomNavigationView.getLayoutParams();
        layoutParams.setBehavior(new BottomNavigationViewBehavior());
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        return selectFragment(item);
    }

    @Override
    public void onBackPressed() {
        MenuItem homeItem = mBottomNavigationView.getMenu().getItem(0);
        if (mSelectedItem != homeItem.getItemId()) {
            selectFragment(homeItem);
            mBottomNavigationView.setSelectedItemId(0);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(SELECTED_ITEM, mSelectedItem);
        super.onSaveInstanceState(outState);
    }

    //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.container, fragment).hide(fragment).commit();
    }

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

    //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(bookClubFragment);
        addHideFragment(rewardsFragment);
        addHideFragment(storeFragment);
        addHideFragment(profileFragment);

        mFragmentManager.beginTransaction().add(R.id.container, myBooksFragment).commit();
        mFragment = myBooksFragment;
    }

    private boolean selectFragment(MenuItem item) {
        if (mSelectedItem == item.getItemId()) {
            return false;
        }

        switch (item.getItemId()) {
            case R.id.navigation_my_books:
                mFragment = hideShowFragment(mFragment, myBooksFragment);
                break;
            case R.id.navigation_store:
                mFragment = hideShowFragment(mFragment, storeFragment);
                break;
            case R.id.navigation_book_club:
                mFragment = hideShowFragment(mFragment, bookClubFragment);
                break;
            case R.id.navigation_rewards:
                mFragment = hideShowFragment(mFragment, rewardsFragment);
                break;
            case R.id.navigation_profile:
                mFragment = hideShowFragment(mFragment, profileFragment);
                break;
        }

        mSelectedItem = item.getItemId();
        return true;
    }

    @Override
    public void recyclerViewListClicked(View v, int position) {

    }
}

以上是关于java BottomBarNavigation代码保留片段状态的主要内容,如果未能解决你的问题,请参考以下文章

java毕业设计成品代做

java代写,java程序代写,代写java,java编程代写

java内存模型 年轻代/年老代 持久区

寻找基于javaweb的毕业设计代做?请找我们

Java堆内存

代写java binary search trees|代写Java Data Structures CS作业|代写Java作业|Java 编程作业代写|Java作业代写