XIAO391 2016-09-26 06:28 采纳率: 100%
浏览 960
已采纳

viewpager中两个tabhost互相冲突

viewpager页面里,2个靠近的页面都使用了tabhost,可以正常运行,但是后初始化的那个界面的tabhost会部署到
先初始化完成的那个页面里。
我是这么完成tabhost的

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

        tabHost5 = (TabHost) view.findViewById(R.id.tabHost1);
        tabHost5.setup();

        tabHost5.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tab) {
                System.out.println("current tabid=" + tab);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                if (TextUtils.equals("3", tab)) {
                    //add/replace fragment first
                    frag5 = new frg5_frg1();
                    System.out.println("load f3");
                } else if (TextUtils.equals("4", tab)) {
                    //add/replace fragment second
                    frag5 = new frg5_frg2();
                    System.out.println("load f4");
                } else if (TextUtils.equals("5", tab)) {
                    //add/replace fragment second
                    frag5 = new frg5_frg3();
                    System.out.println("load f5");
                }

                ft.replace(android.R.id.tabcontent, frag5, "frag5");
                ft.commit();
            }
        });

        tabHost5.addTab(tabHost5.newTabSpec("3").setIndicator("环境设置")
                .setContent(new DummyTabFactory(getActivity())));
        tabHost5.addTab(tabHost5.newTabSpec("4").setIndicator("安全设置")//setIndicator 设置标签样式
                .setContent(new DummyTabFactory(getActivity())));//setContent 点击标签后触发
        tabHost5.addTab(tabHost5.newTabSpec("5").setIndicator("密码设置")//setIndicator 设置标签样式
                .setContent(new DummyTabFactory(getActivity()))); //setContent 点击标签后触发

        return view;
    }

    private static class DummyTabFactory implements TabHost.TabContentFactory {
        private Context context;
        private DummyTabFactory(Context ctx) {
            this.context = ctx;
        }
        @Override
        public View createTabContent(String tag1) {//创建宽高均为0的view
            View v = new ImageView(context);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }
}

布局页面

 <?xml version="1.0" encoding="utf-8"?>
    <TabHost  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tabHost1">

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

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

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


            </FrameLayout>
        </LinearLayout>
    </TabHost>

2个tabhost都是这么完成的,但是因为必须用@android:id/tabcontent。所以2个界面的布局就变得重复了,
后加载的那个tabhost会把前面的盖掉。我想过把viewpager的预加载关掉,那样就不会提前初始化下一个tabhost,但学的太少,去掉预加载老是出问题。

现在为了实现功能,把一个tabhost改成了取消滑动的viewpager,虽然看着像,但终究不是tabhost。
tabhost的实现方法问题吗?

  • 写回答

1条回答 默认 最新

  • 鱼弦 全栈领域优质创作者 2016-09-26 07:47
    关注

    设置联动监听应该不会出现

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序