FragmentTabHost底部切换

移动开发 waitig 494℃ 百度已收录 0评论

private void initFragmentTabHost() {
//枚举方式创建
mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.fl);
//去掉分割线
if (Build.VERSION.SDK_INT > 10) {
mFragmentTabHost.getTabWidget().setShowDividers(0);
}

    for (int i = 0; i < EMAINTABINFO.values().length; i++) {
        EMAINTABINFO[] values = EMAINTABINFO.values();
        EMAINTABINFO value = values[i];
        String title = value.title;
        int topResId = value.topResId;
        Class clz = value.clz;

        TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(title);

        View indicator = View.inflate(this, R.layout.view_indicator1, null);
        TextView tabTitle = indicator.findViewById(R.id.tab_title);
        tabTitle.setText(title);

        tabTitle.setCompoundDrawablesWithIntrinsicBounds(0, topResId, 0, 0);

        tabSpec.setIndicator(indicator);
        Bundle bundle = new Bundle();
        bundle.putString("args", "" + i);
        mFragmentTabHost.addTab(tabSpec, clz, bundle);
        if (i == 2) {
            indicator.setVisibility(View.INVISIBLE);
        }
    }
}

public enum EMAINTABINFO {
    NEW("综合", R.drawable.tab_icon_new, NewsTabFragment.class),
    TWEET("动弹", R.drawable.tab_icon_tweet, TweetTabFragment.class),
    QUICKOPTION("", R.drawable.tab_icon_explore, TweetTabFragment.class),
    EXPLORE("发现", R.drawable.tab_icon_explore, ExploreTabFragment.class),
    ME("我", R.drawable.tab_icon_me, MeTabFragment.class);
    public String title;
    public int    topResId;
    public Class  clz;

    EMAINTABINFO(String title, int topResId, Class clz) {
        this.title = title;
        this.topResId = topResId;
        this.clz = clz;
    }
}

//
private void 集合方式创建FragmentTabHost() {
mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.fl);
//去掉分割线
if (Build.VERSION.SDK_INT > 10) {
mFragmentTabHost.getTabWidget().setShowDividers(0);
}
List mainTabInfos = new ArrayList<>();
mainTabInfos.add(new MainTabInfo(“综合”, R.drawable.tab_icon_new, NewsPagerFragment.class));
mainTabInfos.add(new MainTabInfo(“动弹”, R.drawable.tab_icon_tweet, TweetPagerFragment.class));
mainTabInfos.add(new MainTabInfo(“”, R.drawable.tab_icon_explore, TweetPagerFragment.class));
mainTabInfos.add(new MainTabInfo(“发现”, R.drawable.tab_icon_explore, TweetPagerFragment.class));
mainTabInfos.add(new MainTabInfo(“我”, R.drawable.tab_icon_me, TweetPagerFragment.class));
for (int i = 0; i < mainTabInfos.size(); i++) {
String title = mainTabInfos.get(i).title;
int topResId = mainTabInfos.get(i).topResId;
Class clz = mainTabInfos.get(i).clz;

        TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(title);

        View indicator = View.inflate(this, R.layout.view_indicator1, null);
        TextView tabTitle = indicator.findViewById(R.id.tab_title);
        tabTitle.setText(title);

        tabTitle.setCompoundDrawablesWithIntrinsicBounds(0, topResId, 0, 0);

        tabSpec.setIndicator(indicator);
        Bundle bundle = new Bundle();
        bundle.putString("args", "" + i);
        mFragmentTabHost.addTab(tabSpec, clz, bundle);
        if (i == 2) {
            indicator.setVisibility(View.INVISIBLE);
        }
    }
}

class MainTabInfo {
    public String title;
    public int    topResId;
    public Class  clz;

    public MainTabInfo(String title, int topResId, Class clz) {
        this.title = title;
        this.topResId = topResId;
        this.clz = clz;
    }
}

//
private void 初始化数组方式FragmentTabHost() {
mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.fl);
//去掉分割线
if (Build.VERSION.SDK_INT > 10) {
mFragmentTabHost.getTabWidget().setShowDividers(0);
}
String mainTileArr[] = new String[]{“综合”, “动弹”, “”, “发现”, “我”};
int topResIdArr[] = new int[]{
R.drawable.tab_icon_new,
R.drawable.tab_icon_tweet,
R.drawable.tab_icon_explore,
R.drawable.tab_icon_explore,
R.drawable.tab_icon_me,
};
for (int i = 0; i < mainTileArr.length; i++) {
String title = mainTileArr[i];
int topResId = topResIdArr[i];
TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(title);
View indicatorView = View.inflate(this, R.layout.view_indicator1, null);
TextView tabTitle = indicatorView.findViewById(R.id.tab_title);
tabTitle.setCompoundDrawablesWithIntrinsicBounds(0, topResId, 0, 0);
//设置tabSpec的样式
tabSpec.setIndicator(indicatorView);
Bundle bundle = new Bundle();
bundle.putString(“args”, “” + i);
if (i == 2) {
indicatorView.setVisibility(View.INVISIBLE);
}
mFragmentTabHost.addTab(tabSpec, NewsPagerFragment.class, bundle);
}
}
//
private void 方式一初始化FragmentTabHost() {
mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.fl);
View IndicatorView1 = View.inflate(this, R.layout.view_indicator1, null);
TextView tab_title1 = IndicatorView1.findViewById(R.id.tab_title);
tab_title1.setText(“综合”);
tab_title1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.tab_icon_new, 0, 0);

    TabHost.TabSpec tab1 = mFragmentTabHost.newTabSpec("tag1");//添加tab标签
    tab1.setIndicator(IndicatorView1);//设置样式
    Bundle bundle = new Bundle();
    bundle.putString("args", "one");
    mFragmentTabHost.addTab(tab1, NewsPagerFragment.class, bundle);

    View IndicatorView2 = View.inflate(this, R.layout.view_indicator1, null);
    TextView tab_title2 = IndicatorView1.findViewById(R.id.tab_title);
    tab_title2.setText("动弹");
    tab_title2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.tab_icon_tweet, 0, 0);

    TabHost.TabSpec tab2 = mFragmentTabHost.newTabSpec("tag2");//添加tab标签
    tab2.setIndicator(IndicatorView2);//设置样式
    Bundle bundle2 = new Bundle();
    bundle2.putString("args", "two");
    mFragmentTabHost.addTab(tab2, NewsPagerFragment.class, bundle2);
}
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>


本文由【waitig】发表在等英博客
本文固定链接:FragmentTabHost底部切换
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (1)分享 (0)