我将框架布局放在一个活动中,并将片段数放入框架布局。
我的自定义抽屉菜单中的片段开关。
其中一个片段,它有一个tabLayout和四个要切换的标签。
所以我的问题是当我切换回片段时,标签会显示dealy。
我尝试将代码放入transaction.addToBackStack(null);它没有用。
请教我如何解决这个延迟,谢谢。
这是我的代码:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (id == R.id.homePage) {
Homepage homepageFragment = new Homepage();
transaction.addToBackStack(null);
transaction.replace(R.id.mainFrame, homepageFragment, null);
setTitleBar(R.string.appTitle);
transaction.commit();
} else if (id == R.id.newestInformation) {
NewsInformation newsFragment = new NewsInformation();
transaction.addToBackStack(null);
transaction.replace(R.id.mainFrame, newsFragment, null);
setTitleBar(R.string.newestInformation);
transaction.commit();
}
//的setContentView
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Homepage homepageFragment = new Homepage();
Fragment fragment = manager.findFragmentById(R.id.mainFrame);
if (fragment == null) {
transaction.addToBackStack(null);
transaction.add(R.id.mainFrame, homepageFragment, null);
setTitleBar(R.string.appTitle);
transaction.commit();
} else {
Log.i(TAG, ">>>" + "homepageFragment!=null");
}
// XML
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<android.support.v7.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/ToolBarStyle"
android:background="@color/colorAccent">
<TextView
android:id="@+id/textTitle"
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/background_light" />
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/mainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
//片段xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:id="@+id/tabLayoutHomePage"
android:layout_weight="1"
app:tabTextColor="@android:color/white"
android:background="@color/colorPrimary"
android:layout_height="80dp">
</android.support.design.widget.TabLayout>
<view
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="android.support.v4.view.ViewPager"
android:id="@+id/viewPager"
android:layout_weight="1" />
</android.support.design.widget.AppBarLayout>
答案 0 :(得分:0)
我找到了解决方案,因为我的ViewPager在nest片段上。 我必须getChildFragmentManager,所以我像这样chabge viewpager代码: ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter (getChildFragmentManager(),tabLayoutHomePage.getTabCount());