0

I have an android interface use coordinatorLayout,AppBarLayout and NestedScrollView to manage the scroll action.

Before scroll Snapshot

After scroll Snapshot

How do I show the AppbarLayout to the top of the screen when I clicked the comment button after scroll?Just like before scroll.

Here is my layout:

<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ececec">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f7f7f7"
        android:fitsSystemWindows="true"
        android:minHeight="1dp"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlwaysCollapsed">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@color/color_3"
            android:layout_margin="10dp"/>
        <!--<include layout="@layout/wentixiangqing_dingbu_wentidaan"/>-->

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#FFF">
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="star:123"
                android:textColor="#797979"/>
            <View
                android:layout_width="30dp"
                android:layout_height="3dp"
                android:layout_gravity="bottom|center_horizontal"
                android:background="#F00"/>
        </FrameLayout>
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="20dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="comment:90"
                android:textColor="#797979"/>
            <View
                android:layout_width="30dp"
                android:layout_height="3dp"
                android:layout_gravity="bottom|center_horizontal"
                android:background="#F00"/>
        </FrameLayout>
    </LinearLayout>

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="40dp"
        android:layout_marginTop="1dp"
        android:overScrollMode="always" />

</android.support.v4.widget.NestedScrollView>

<Button
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_gravity="bottom|end"
    android:text="Comment" /></android.support.design.widget.CoordinatorLayout>

Any suggestion would be appreciated.

Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
Deng
  • 58
  • 5

1 Answers1

1

have you tried this code on clicking the button ?

AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appbar);
            appBarLayout.setExpanded(true, true);

Also see this : Hide/Show Toolbar programmatically on CoordinatorLayout

Umar Hussain
  • 3,461
  • 1
  • 16
  • 38
  • Thanks for your advice,but it doesn't work for me.I want to display appbarlayout when user pressed the comment button only. – Deng Aug 23 '17 at 07:54
  • have you put this call in the click listener of your button ? `appBarLayout.setExpanded(true, true);` – Umar Hussain Aug 23 '17 at 07:56
  • 1
    emmmm,sorry,I've tried again and it works!Thanks a lot for your advice! – Deng Aug 23 '17 at 07:59