android-片段中的标签视图

android-片段中的标签视图,第1张

概述我一直试图在片段中使用选项卡视图,但是通过导航栏访问它时会出现一些错误.第一次访问它时,我可以在选项卡之间自由移动,但是当再次访问此页面时从另一个片段开始,选项卡不再移动,我只停留在单个选项卡中,默认情况下仅显示第一个选项卡.请帮助.这是我的BlankFragment3.java代码:p

我一直试图在片段中使用选项卡视图,但是通过导航栏访问它时会出现一些错误.第一次访问它时,我可以在选项卡之间自由移动,但是当再次访问此页面时从另一个片段开始,选项卡不再移动,我只停留在单个选项卡中,默认情况下仅显示第一个选项卡.请帮助.

这是我的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];    }}

解决方法:

试试这个

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>
总结

以上是内存溢出为你收集整理的android-片段中的标签视图全部内容,希望文章能够帮你解决android-片段中的标签视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1079698.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存