简体   繁体   中英

How to scroll CoordinatorLayout's content to the top?

The ViewPager has two Fragment s,one Fragment 's layout contains a NestedScrollView , another one's layout contains a RecyclerView , when I scroll the NestedScrollView to the bottom, here is a demand that user click the whole title region all CoordinatorLayout 's content must scroll to top same as the beginning. but I don't know how to do that with CoordinatorLayout . it seems that there's no api method that support my demand.

Here is my layout xml:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinate_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:fitsSystemWindows="true"
        android:background="?attr/color_26"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <android.support.v7.widget.Toolbar
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </android.support.v7.widget.Toolbar>


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

        <android.support.design.widget.TabLayout

            app:tabTextColor="?attr/color_8"
            app:tabSelectedTextColor="?attr/color_18"
            app:tabIndicatorHeight="0dp"
            app:tabGravity="fill"
            app:tabMode="fixed"
            android:layout_width="match_parent"
            android:layout_height="42dp">

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

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


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

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

when I scroll the NestedScrollView to the bottom, here is a demand that user click the whole title region all CoordinatorLayout 's content must scroll to top same as the beginning. but I don't know how to do that with CoordinatorLayout

You can handle that when user clicked on the whole title region (Maybe clicking on the custom Toolbar 's TextView ), Reload-refreshing the Activity :

finish();
startActivity(getIntent());

This will make the content start from top just like the beginning.

Or, try this:

findViewById(R.id.recycler_view).setFocusable(false);
findViewById(R.id.temp).requestFocus();

Check this answer for that: https://stackoverflow.com/a/33584361/4409113

However, you won't need the CollapsingToolbarLayout since you just have TabLayout and Toolbar and there is no ImageView or a parallax view in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM