Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

android - Replacing Fragment Inside of ViewPager Opens the Fragment in ViewPager Dimensions

Fragment A contains ViewPager which has Fragment B. Fragment B contains a RecyclerView and clicking on its items, opens Fragment C.

Open Fragment C (implemented by Fragment 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();
    }

Fragment A Layout

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

Fragment B Layout

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

Fragment C Layout

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

The problem is, Fragment C content is shown inside of the ViewPager not whole Fragment A layout.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 a FrameLayout for your FragmentTransaction that is also a child of your layout_root.

Quoted from https://stackoverflow.com/a/18885196/3749773. Thanks to Paul Burke

Another mistake was calling getChildFragmentManager() from Fragment B but I had to call it from Fragment A. Thanks to frozenkoi


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...