Creating Tabs inside Fragment

浪尽此生 提交于 2020-01-01 19:55:49

问题


I'm getting a fragment from mother activity. Now i want to create tabs in this fragment. But tabHost.setup() method showing error. I don't get it so need a clue about error.

// FragmentOne.java

package com.example.sharelocationui;

import android.app.Fragment;   
import android.os.Bundle;  
import android.support.v4.app.FragmentTabHost;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.webkit.WebView.FindListener;  
import android.widget.ImageView;  
import android.widget.TextView;  
import android.widget.TabHost;  

public class FragmentOne extends Fragment {


private FragmentTabHost tabHost;

public FragmentOne() {

}

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

    super.onCreateView(inflater, container, savedInstanceState); 

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

    tabHost = (FragmentTabHost) v.findViewById(R.id.testtabhost1);

    tabHost = new FragmentTabHost(getActivity());
    tabHost.setup(getActivity(),getChildFragmentManager(),R.id.testtabhost1); 

    tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("TAB1")); 
    tabHost.addTab(tabHost.newTabSpec("tab_test2").setIndicator("TAB2")); 
    tabHost.addTab(tabHost.newTabSpec("tab_test3").setIndicator("TAB3")); 
    tabHost.setCurrentTab(0); 

    return v; 
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    tabHost = null;
}

}

//fragment_layout_one

<android.support.v4.app.FragmentTabHost 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testtabhost1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </FrameLayout>
</LinearLayout>

</android.support.v4.app.FragmentTabHost>

Error showing

'The method setup(Context, FragmentManager, int) in the type FragmentTabHost is not applicable for the arguments (Activity, FragmentManager, int)'

回答1:


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> 



回答2:


Your problem is you are importing

android.app.Fragment

not

android.support.v4.app.Fragment



回答3:


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




回答4:


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;
    }
}


来源:https://stackoverflow.com/questions/25479980/creating-tabs-inside-fragment

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