lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cn.sinata.xldutils.widget.utils;
 
/**
 *
 * Created by LiaoXiang on 2015/11/18.
 */
 
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
 
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
 
import java.util.HashMap;
 
import cn.sinata.xldutils.activitys.BaseActivity;
 
/**
 * 标签管理
 *
 * @author Administrator
 *
 */
public class TabManager implements TabHost.OnTabChangeListener {
    private final BaseActivity mActivity;
    private final TabHost mTabHost;
    private final int mContainerId;
    private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
    TabInfo mLastTab;
 
    static final class TabInfo {
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;
        private Fragment fragment;
 
        TabInfo(String _tag, Class<?> _class, Bundle _args) {
            tag = _tag;
            clss = _class;
            args = _args;
        }
    }
 
    /**
     * 标签生成
     *
     * @author Administrator
     *
     */
    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;
 
        public DummyTabFactory(Context context) {
            mContext = context;
        }
 
        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }
 
    public TabManager(BaseActivity activity, TabHost tabHost,
                      int containerId) {
        mActivity = activity;
        mTabHost = tabHost;
        mContainerId = containerId;
        mTabHost.setOnTabChangedListener(this);
    }
 
    /**
     * 添加标签
     *
     * @param tabSpec
     * @param clss
     * @param args
     */
    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
        tabSpec.setContent(new DummyTabFactory(mActivity));
        String tag = tabSpec.getTag();
        TabInfo info = new TabInfo(tag, clss, args);
 
        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state. If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        info.fragment = mActivity.getSupportFragmentManager()
                .findFragmentByTag(tag);
        if (info.fragment != null && !info.fragment.isDetached()) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager()
                    .beginTransaction();
            ft.detach(info.fragment);
            ft.commitAllowingStateLoss();
        }
 
        mTabs.put(tag, info);
        mTabHost.addTab(tabSpec);
    }
 
    @Override
    public void onTabChanged(String tabId) {
        // tab标签切换操作
        TabInfo newTab = mTabs.get(tabId);
 
        if (mLastTab != newTab) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager()
                    .beginTransaction();
            if (mLastTab != null) {
                // 如果当前标签的fragment不为空就隐藏
                if (mLastTab.fragment != null) {
                    // ft.detach(mLastTab.fragment);
                    ft.hide(mLastTab.fragment);// 隐藏当 前fragment
                }
            }
            if (newTab != null) {
                // 如果需要切换的标签的fragment为空就add新的fragment。反之则显示
 
                if (newTab.fragment == null || newTab.fragment.isDetached()) {
 
                    newTab.fragment = Fragment.instantiate(mActivity,
                            newTab.clss.getName(), newTab.args);
                    ft.add(mContainerId, newTab.fragment, newTab.tag);
                } else {
                    // ft.attach(newTab.fragment);//google在FragmentTabs中使用的方法,会调用onCreateView()重绘视图
                    ft.show(newTab.fragment);// 显示fragment
                }
            }
 
            mLastTab = newTab;
            ft.commitAllowingStateLoss();
            mActivity.getSupportFragmentManager().executePendingTransactions();
        }
    }
}