替换ViewPager内部的片段会在ViewPager维度中打开片段 [英] Replacing Fragment Inside of ViewPager Opens the Fragment in ViewPager Dimensions
问题描述
片段A包含具有片段B的ViewPager
.片段B包含RecyclerView
,然后单击其项目,打开片段C.
Fragment A contains ViewPager
which has Fragment B. Fragment B contains a RecyclerView
and clicking on its items, opens Fragment C.
打开片段C(由片段B实现)
private void openItemPage(long id) {
Fragment a2Fragment = CompanyPageFragment.newInstance(id);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
// store the Fragment in stack
transaction.addToBackStack(null);
transaction.replace(R.id.container, a2Fragment).commit();
}
片段A布局
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="15dp">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@drawable/top_bottom_border"
app:tabBackground="@android:color/transparent"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed">
</android.support.design.widget.TabLayout>
<ir.barandeh.android.views.NonSwipeableViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"/>
</LinearLayout>
片段B布局
<RelativeLayout android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.ContentLoadingProgressBar
android:id="@+id/contentLoading"
style="?android:attr/android:progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:paddingEnd="10dp"
android:paddingStart="10dp">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
片段C布局
<RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/windowBackground">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ir.barandeh.android.views.NonSwipeableViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"/>
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
问题是,片段C的内容显示在ViewPager
的内部,而不是整个片段A的布局.
The problem is, Fragment C content is shown inside of the ViewPager
not whole Fragment A layout.
推荐答案
您正在尝试以编程方式替换
LinearLayout
,同时 期望它有硬编码的孩子.这行不通.你 应该为您的FragmentTransaction
使用FrameLayout
您layout_root
的孩子.
You're trying to programmatically replace a
LinearLayout
, while also expecting it to have the hard-coded children. This won't work. You should use aFrameLayout
for yourFragmentTransaction
that is also a child of yourlayout_root
.
引自 https://stackoverflow.com/a/18885196/3749773 .感谢 Paul Burke
另一个错误是从片段B调用getChildFragmentManager()
,但是我不得不从片段A调用它.感谢 frozenkoi
Another mistake was calling getChildFragmentManager()
from Fragment B but I had to call it from Fragment A. Thanks to frozenkoi
这篇关于替换ViewPager内部的片段会在ViewPager维度中打开片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!