在一个片段中的选项卡视图

3
我一直在尝试在片段中使用选项卡视图,但是通过导航栏访问时会出现一些错误。第一次访问时,我可以自由地在选项卡之间移动,但是当从另一个片段再次返回到此页面时,选项卡不再移动,而且我只能卡在一个选项卡中,默认情况下只显示第一个选项卡。请帮忙解决。
以下是我的BlankFragment3.java代码:
public class BlankFragment3 extends android.support.v4.app.Fragment {

    FragmentManager fragmentManager;
    private MainActivity myContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        myContext= (MainActivity) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_blank_fragment3, container, false);


        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        viewPager.setAdapter(new SampleFragmentPagerAdapter(myContext.getSupportFragmentManager()));
        PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip)view.findViewById(R.id.tabs);
        tabsStrip.setBackgroundColor(Color.parseColor("#333333"));
        // Attach the view pager to the tab strip
        tabsStrip.setViewPager(viewPager);
        return view;
    }
}

这是我的fragment.xml文件:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar">
    </include>

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        app:pstsShouldExpand="true"
        app:pstsTextAllCaps="true"
        app:pstsIndicatorColor="#ff9900"
        android:textColor="#ffffff"
        android:textSize="14sp"
        app:pstsUnderlineColor="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="48dp">
    </com.astuetz.PagerSlidingTabStrip>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white" />

</LinearLayout>

这里是SampleFragmentPagerAdapter.java的代码:

public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 2;
    private String tabTitles[] = new String[] { "Today's Deals", "Deals Close By" };

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

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public Fragment getItem(int position) {
        if(position==0) {
            return new TodaysDeal();
        } else {
            return new DealsCloseBy();
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}

你尝试过使用FragmentTabHost吗? - Aditya
必须至少查看错误才能知道。也许还有您的Activity。 - milez
@milez--这是我的MainActivity代码,已添加在上方。 - Animesh Jena
2个回答

3
尝试这种方法。
public class HomeFragment extends Fragment {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    private FragmentTabHost tabHost;

    public HomeFragment(){}

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

        tabHost = new FragmentTabHost(getActivity());
        tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);

        Bundle arg1 = new Bundle();
        arg1.putInt("Arg for Frag1", 1);
        tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("Tab1")),
                FragmentA.class, arg1);

        Bundle arg2 = new Bundle();
        arg2.putInt("Arg for Frag2", 2);
        tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("Tab 2")),
            FragmentB.class, arg2);


        return tabHost;

    }

XML

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

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

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

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

@Roman,请问您能告诉我在 my_Parent_fragment.xml 中要写什么吗? - Animesh Jena
@Roman--谢谢你..但是在渲染过程中仍然出现了一些异常。在渲染过程中引发的异常:未知标签null。 - Animesh Jena
渲染期间引发异常:未知标签“nul”。 - Animesh Jena
清理你的项目并运行它。 - Aditya
@Roman-它仍然存在,并且在运行XML预览之前出现。 - Animesh Jena
1
希望这个能更好地帮助你...https://github.com/Balaji-K13/Navigation-drawer-page-sliding-tab-strip - Aditya

0
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="match_parent" >

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

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

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接