Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments inside NestedScrollView

I'm trying to put fragments into NestedSrollView. Since it's child of FrameLayout I assume It's possible. This is xml:

<android.support.v4.widget.NestedScrollView
    android:id="@+id/fragmentContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.widget.NestedScrollView>

After first transaction everything is fine, fragment is attached to NestedScrollView:

getSupportFragmentManager()
            .beginTransaction()
            .setCustomAnimations(R.anim.slide_right, R.anim.fade_out, R.anim.slide_left, R.anim.fade_out)
            .add(R.id.fragmentContainer, new RankingFragment(), RANKING_FRAGMENT)
            .commit();

But then when I want to replace first fragment I get this error:

ScrollView can host only one direct child

Second transaction:

getSupportFragmentManager()
                .beginTransaction()
                .setCustomAnimations(R.anim.slide_right, R.anim.fade_out, R.anim.slide_left, R.anim.fade_out)
                .replace(R.id.fragmentContainer, new com.steveq.photoquiz.ui.fragments.PreparationFragment(), PREPARATION_FRAGMENT)
                .addToBackStack(PREPARATION_FRAGMENT)
                .commit();

Can anyone help with this?

like image 419
adams172 Avatar asked Feb 10 '17 16:02

adams172


1 Answers

Try to set this attr 'android:fillViewport' as true, then you will find your fragment is attached to activity

<android.support.v4.widget.NestedScrollView
    android:id="@+id/feed_club_tab_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/feedui_pager_tab"
    android:fillViewport="true"
    app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"/>
like image 62
Zhang Xiang Avatar answered Nov 15 '22 17:11

Zhang Xiang