FragmentPresenter.java 3.44 KB
package com.lijinji.part.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.lijinji.part.R;
import com.lijinji.part.product.PartFillingFragment;
import com.lijinji.part.product.PartManagerFragment;

/**
 * Created by youjie on 2018/4/28.
 */

public class FragmentPresenter {

    private FragmentManager fragmentManager;
    private FragmentTransaction mFragmentTransaction;
    private String[] strarr;

    public String[] getStrarr() {
        return strarr;
    }

    public void setStrarr(String[] strarr) {
        this.strarr = strarr;
    }

    public FragmentManager getFragmentManager() {
        return fragmentManager;
    }

    public void setFragmentManager(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    public void selectPage(int start, int end) {
        if (start == end) {
            return;
        }

        if (start < strarr.length && end < strarr.length) {
            String cur = getTag(start, strarr);
            String old = getTag(end, strarr);
            hideAllFragment();
            showFragment(cur, old);
        }
    }

    public String getTag(int index, String[] array) {
        String tag = "";
        if (index > -1 && index < array.length) {
            tag = array[index];
        }
        return tag;
    }

    private FragmentTransaction initTransaction() {
        if (mFragmentTransaction == null) {
            mFragmentTransaction = fragmentManager.beginTransaction();
            mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        }
        return mFragmentTransaction;
    }

    private void commitTransactions() {
        if (mFragmentTransaction != null && !mFragmentTransaction.isEmpty()) {
            mFragmentTransaction.commitAllowingStateLoss();
            mFragmentTransaction = null;
        }
    }

    private void showFragment(String oldTag, String newTag) {
        initTransaction();
        Fragment oldFragment = getFragment(oldTag);
        Fragment newFragment = getFragment(newTag);
        if (newFragment == null || !newFragment.isAdded()) {
            newFragment = getFragment(newTag);
            mFragmentTransaction.add(R.id.content, newFragment, newTag);
            mFragmentTransaction.addToBackStack(newTag);
        }
        if (oldFragment != null) {
            mFragmentTransaction.hide(oldFragment);
        }

        if (newFragment != null) {
            mFragmentTransaction.show(newFragment);
        }
        commitTransactions();
    }

    private void hideAllFragment() {
        initTransaction();
        for (String str : strarr) {
            Fragment fragment = fragmentManager.findFragmentByTag(str);
            if (fragment != null) {
                mFragmentTransaction.hide(fragment);
            }
        }
        commitTransactions();
    }

    public Fragment getFragment(String tag) {
        Fragment fragment = fragmentManager.findFragmentByTag(tag);
        if (fragment == null) {
            fragment = newFragment(tag);
        }
        return fragment;
    }

    private BaseFragment newFragment(String tag) {
        BaseFragment baseFragment = null;
        if (tag.equals(strarr[0])) {
            baseFragment = new PartManagerFragment();
        }  else if (tag.equals(strarr[1])) {
            baseFragment = new PartFillingFragment();
        }
        return baseFragment;
    }
}