高仿APP——元贝驾考(一)FragmentUtils


先大概看一下项目结构

高仿APP——元贝驾考(一)FragmentUtils_第1张图片

adaprter和base就不用说了,就是适配器和baseActivity之类的东西


高仿APP——元贝驾考(一)FragmentUtils_第2张图片

callBack里是一些 监听和回调接口


高仿APP——元贝驾考(一)FragmentUtils_第3张图片

属性 实体类



重点看一下FragmentUtil.class

package com.zzyd.utils;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 构造器;
 * FragmentUtil();
 * onSaveInstanceState();
 */

public class FragmentUtil {

    private static final String CURRENT_FRAGMENT = "STATE_FRAGMENT_SHOW";

    private FragmentManager fragmentManager;

    private Fragment currentFragment = new Fragment();
    private List<Fragment> fragments = new ArrayList<>();

    /**
     * 当前fragment的下标
     */
    private int currentIndex = 0;

    /**
     * Fragment的载体view容器
     */
    private int containerViewId;

    public FragmentUtil(AppCompatActivity appCompatActivity, int containerViewId){
        this.fragmentManager = appCompatActivity.getSupportFragmentManager();
        this.containerViewId = containerViewId;
    }

    public void initFragment(Bundle savedInstanceState,InitFragmentCallBack initFragmentCallBack){
        if (savedInstanceState != null) {
            // “内存重启”时调用
            //获取“内存重启”时保存的索引下标
            currentIndex = savedInstanceState.getInt(CURRENT_FRAGMENT,0);
            fragments.removeAll(fragments);
            for(int i = 0 ; i < fragments.size() ; i ++){
                fragments.add(fragmentManager.findFragmentByTag(i+""));
            }
            //恢复fragment页面
            restoreFragment();
        }else{
            //正常启动时调用
            try {
                initFragmentCallBack.initFragment(fragments);
            }catch (NullPointerException e){
                Log.e("","FragmentUtil:initFragmentCallBack == null");
            }
            showFragment();
        }
    }


    public void onSaveInstanceState(Bundle outState){
        //“内存重启”时保存当前的fragment名字
        outState.putInt(CURRENT_FRAGMENT,currentIndex);
    }

    /**
     * 使用show() hide()切换页面
     * 显示fragment
     */
    public void showFragment(){
        if(containerViewId == 0){
            Log.e("","containerViewId == null");
            return;
        }
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //如果之前没有添加过
        if(!fragments.get(currentIndex).isAdded()){
            transaction.hide(currentFragment).add(containerViewId,fragments.get(currentIndex),""+currentIndex);  //第三个参数为添加当前的fragment时绑定一个tag
        }else{
            transaction.hide(currentFragment).show(fragments.get(currentIndex));
        }
        currentFragment = fragments.get(currentIndex);
        transaction.commit();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);//标准动画
    }

    /**
     * 恢复fragment
     */
    public void restoreFragment(){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
            if(i == currentIndex){
                transaction.show(fragments.get(i));
            }else{
                transaction.hide(fragments.get(i));
            }
        }
        transaction.commit();
        //把当前显示的fragment记录下来
        currentFragment = fragments.get(currentIndex);
    }

    /**
     * 切换Fragment
     * @param currentIndex
     */
    public void selectFragment(int currentIndex){
        this.currentIndex = currentIndex;
        showFragment();
    }

    public boolean isShowing(int index){
        if(index == this.currentIndex){
            return true;//当前Fragment处于显示状态
        }else{
            return false;
        }
    }

    public int getCurrentIndex(){
        return currentIndex;
    }

    public List<Fragment> getFragments(){
        return fragments;
    }

    public interface InitFragmentCallBack {
        void initFragment(List<Fragment> fragments);
    }

}



你可能感兴趣的:(Android)