替换android片段时出现问题

用户名

是)我有的:

我正在使用API​​ 22上的最新Android支持和设计库。我有一个名为ProgressFragment的片段该片段仅在首次安装我的应用程序时才运行,如下所示:

mProgressFragment = new ProgressFragment();
getSupportFragmentManager().beginTransaction()
                .replace(R.id.main_container, mProgressFragment, TAG_TASK_FRAGMENT)
                .commit();

这是布局

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progress_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/toolbar"/>

<LinearLayout

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    >

    <ProgressBar
        android:id="@+id/build_lib_progressbar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/progress_msg1"
        android:layout_gravity="center"
        android:textSize="22sp"
        android:textColor="@color/text_primary"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/progress_msg2"
        android:layout_gravity="center"
        android:paddingTop="10dp"
        android:textColor="@color/text_secondary"/>

    </LinearLayout>

</LinearLayout>

该片段包含一个AsyncTask完成后,将其替换为AActivTask回调onPostExecute()MainActivity中的另一个片段(称为MainFragment),如下所示:

getSupportFragmentManager().beginTransaction()
            .replace(R.id.main_container, new MainFragment())
            .commit();

这是MainFragment布局:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/main_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/main_appbar_layout"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <include layout="@layout/toolbar" />

    <android.support.design.widget.TabLayout
        android:id="@+id/main_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabIndicatorHeight="4dp"
        app:tabTextColor="@color/white"/>

</android.support.design.widget.AppBarLayout>

编辑:

这是MainActivity的布局:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Main content displayed here -->
    <FrameLayout
        android:id="@+id/main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

<!-- Navigation Drawer -->
<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_drawer_header"
    app:menu="@menu/nav_view_menu"
    android:background="#ffffff"
    app:itemTextColor="@color/text_tertiary"
    app:itemIconTint="#8b8b8b"
    />

问题:

当ProgressFragment中的AsyncTask完成并被MainFragment替换时,TabLayout视图不会显示选项卡:

在此处输入图片说明

我的应用程序在启动时有2条可能的路径。它要么:

  1. 添加ProgressFragment(如果首次运行)->然后由MainFragment代替

  2. 否则添加MainFragment(此处显示选项卡正常)

两种片段替换都发生在MainActivity中。

问题处理路径1

编辑2:

现在在MainActivity中调用recreate()可以“修复”此问题。虽然我不会称其为解决方案。稍后会再说。

马里奥·维拉斯科(Mario velasco)

更新:这是v22.2.1上的错误,已在较新版本中解决

在这里发现问题:https : //code.google.com/p/android/issues/detail?id=180462

简单的解决方法:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章