Creating Tabs inside Fragment

≡放荡痞女 提交于 2019-12-04 20:40:40

This is perfect for your Question

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.comfortstay.R;


public class TabFragment extends Fragment {

    public static TabLayout tabLayout;
    public static ViewPager viewPager;
    public static int int_items = 2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /**
         *Inflate tab_layout and setup Views.
         */
        View v = inflater.inflate(R.layout.fragment_tab_layout, container, false);
        tabLayout = (TabLayout) v.findViewById(R.id.tabs);
        viewPager = (ViewPager) v.findViewById(R.id.viewpager);

        /**
         *Set an Apater for the View Pager
         */
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

        /**
         * Now , this is a workaround ,
         * The setupWithViewPager dose't works without the runnable .
         * Maybe a Support Library Bug .
         */

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                tabLayout.setupWithViewPager(viewPager);
            }
        });

        return v;

    }

    class MyAdapter extends FragmentPagerAdapter {

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

        /**
         * Return fragment with respect to Position .
         */

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new DashboardFragment();
                case 1:
                    return new DashboardFragment();
            }
            return null;
        }

        @Override
        public int getCount() {

            return int_items;

        }

        /**
         * This method returns the title of the tab according to the position.
         */

        @Override
        public CharSequence getPageTitle(int position) {

            switch (position) {
                case 0:
                    String recent_news = "My Order";
                    return recent_news;
                case 1:
                    String category = "Popular Items";
                    return category;
            }
            return null;
        }
    }

}

and layout is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/tab_background_color"
        android:elevation="4dp"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/tabIndicatorColor"
        app:tabIndicatorHeight="5dp"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/tabSelectedTextColor"
        app:tabTextColor="@color/tabTextColor"></android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabs">

    </android.support.v4.view.ViewPager>
</RelativeLayout> 

Your problem is you are importing

android.app.Fragment

not

android.support.v4.app.Fragment

In th onCreateView method just change and remove the

View v = inflater.inflate(R.layout.fragment_layout_one, container,
            false);

return v;

to

return tabhost

UPDATE:

You have not initialized the FragmentTabHost.

 mTabHost = new FragmentTabHost(getActivity());

for reference check the official example

Moshfequr Rahman

But how to call fragment when importing android.support.v4.app.Fragment.

It is part of MainActivity.java :

Button b = (Button) findViewById(R.id.buttonGoTab);

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Fragment fragment = null;
                FragmentManager fragmentManager =getFragmentManager();
                FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
                fragment= new HelloTab(); //Here is error
                fragmentTransaction.replace(android.R.id.content, fragment).commit();
            }
        });

It is a part of HelloTab.java :

import android.app.LocalActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class HelloTab extends Fragment {
    private FragmentTabHost mTabHost;
    public Context context;






    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.hellotablay,container, false);
           context= getActivity().getApplicationContext();

        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
       // @SuppressWarnings("deprecation")
        LocalActivityManager mLocalActivityManager = new LocalActivityManager(this.getActivity(), false);
        mLocalActivityManager.dispatchCreate(savedInstanceState);
        mTabHost.setup(getActivity(),getChildFragmentManager(),android.R.id.tabcontent); 

        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
                Tab11.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"),
               Tab11.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"),
                Tab11.class, null);
    enter code here

        return rootView;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!