Saurabh
Saurabh

Reputation: 1065

Android design Library AppBar scrolling behavior enterAlways

I am using the latest version of the design support library (23.1.1) and I am facing an issue when I use the CollapsingToolbarLayout with the enterAlways scroll flag. Basically, when you scroll back up, the view appears but if also leaves a empty white space at top.

Normal View:

Normal Image view

After scrolling down and then back up (notice the whitespace below status bar): enter image description here

MainActivity.java

public class MainActivity extends AppCompatActivity {

    AppBarLayout appBar;
    View expandedView;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("");

        initViews();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    private void initViews() {
        appBar = (AppBarLayout) findViewById(R.id.appBar);
        appBar.addOnOffsetChangedListener(appBarOffsetChangedListener);

        expandedView = findViewById(R.id.expandedView);

        RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new DummyAdapter());
    }

    private AppBarLayout.OnOffsetChangedListener appBarOffsetChangedListener = new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            int maxOffset = appBar.getTotalScrollRange();
            verticalOffset = Math.abs(verticalOffset);
            if(verticalOffset > maxOffset)
                return;
            float percentage = verticalOffset / (float) maxOffset;

            if(expandedView!=null)
                expandedView.setAlpha(1 - percentage);
        }
    };
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.media2359.fragmenttoolbarchange.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:background="@color/colorPrimaryDark"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin">

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

            <RelativeLayout
                android:id="@+id/expandedView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:paddingLeft="@dimen/toolbar_text_margin_left"
                android:paddingTop="@dimen/toolbar_text_margin_top"
                tools:background="@color/colorPrimaryDark">

                <TextView
                    android:id="@+id/tvName"
                    style="@style/TextAppearance.AppCompat.Headline"
                    android:layout_width="@dimen/toolbar_text_width"
                    android:layout_height="wrap_content"
                    android:text="Hello" />

                <TextView
                    android:id="@+id/tvTime"
                    style="@style/TextAppearance.AppCompat.Body1"
                    android:layout_width="@dimen/toolbar_text_width"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/tvName"
                    android:layout_marginTop="7dp"
                    android:text="04 Feb, Tuesday evening" />

            </RelativeLayout>

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listitem="@layout/item_dummy" />
</android.support.design.widget.CoordinatorLayout>

Using enterAlwaysCollapsed along with enterAlways avoids this issue but I want the full view to come back because in the actual app, the expanded section is way smaller.

Another thing that I have noticed is that, the height of the whitespace is equal to the height to the toolbar.

EDIT 1

I replaced exitUntilCollapsed with snap and then there wasn't any white space but then the toolbar doesn't pin and scrolls away

EDIT 2

Looks like this is an issue with the Design Library: CollapsingToolbarLayout enterAlways not supported

Temporary Workaround: Cheesesquare: enterAlways produces wrong layout

Upvotes: 1

Views: 1992

Answers (1)

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

Perhaps that's because of:

enterAlways

Which the codepath/android_guides says:

enterAlways: The view will become visible when scrolling up. This flag is useful in cases when scrolling from the bottom of a list and wanting to expose the Toolbar as soon as scrolling up takes place.

Maybe you wanna try this: (standard way)

app:layout_scrollFlags="scroll|exitUntilCollapsed"

Honestly, I didn't see somebody is using enterAlways in CollapsingToolbarLayout in my whole development life.Especially, with those two flags:

app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways"

Otherwise, It could be a bug and needs the Google's staffs to answer about it.

Upvotes: 1

Related Questions