简体   繁体   中英

How to use TabHost under android 6.0+

Okay, I decided that using TabHost would be nice in my little app. However, under Android 6.0+, TabActivity and several others have been deprecated. So I have written this:

public class TabMain extends Activity{
@Override
protected void onCreate(Bundle bundle){
    super.onCreate(bundle);

    setContentView(R.layout.tab_main);
    TabHost tabHost = (TabHost)findViewById(R.id.tab_main_tabhost);
    tabHost.setup();//several lines here....}}

And when I run this code, IDE throws me an RuntimtException: "java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?" And I found out that ActivityGroup is also deprecated. How should I bypass this?

try this

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"/>
        <android.support.v4.view.ViewPager
            android:id="@+id/home_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

in your activity

viewPager = (ViewPager) findViewById(R.id.home_viewpager);
        viewPager.setOffscreenPageLimit(4);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

method is

private void setupViewPager(ViewPager viewPager) {
        adapter = new Adapter(getSupportFragmentManager());
        adapter.addFragment(new Store(), "Store");//fragment class name
        adapter.addFragment(new Post(), "GG Connect");//fragment class name


        viewPager.setAdapter(adapter);

    }
    static class Adapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }

您应该使用TabLayout和ViewPager。

Here is an answer I found through hours search on internet Android Tabs with Fragements . I think it's a viable solution here.

ViewPage and TabLayout are in the android.support package meaning they support the backward versions, that's the main reason why I chose not to use it. It's hard to even study the new version of android, and even harder to make backward-compatible code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM