1

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

Community
  • 1
  • 1
Saurabh
  • 1,055
  • 14
  • 38

1 Answers1

1

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.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • But that is exactly what I am trying to achieve, `expose the Toolbar as soon as scrolling up takes place.` I am not sure why it is an issue to use the three of them together. `scroll` to ask it to scroll, `exitUntilCollapsed ` to always keep toolbar in view – Saurabh Feb 04 '16 at 15:41
  • I see, but what happened when you tried these two?: `scroll|exitUntilCollapsed` same results? i said that because there is no implementation about those three flags. – ʍѳђઽ૯ท Feb 04 '16 at 15:46
  • as expected, that disables quick return, i.e. the view only comes back once I have scrolled back to the top of the list. – Saurabh Feb 04 '16 at 16:03
  • I replaced exitUntilCollapsed with snap and then there wasn't any white space but then the toolbar doesn't pin and scrolls away – Saurabh Feb 04 '16 at 16:26
  • I'm using exactly like your try with `snap` and `app:layout_collapseMode="pin"`, it's working, not sure what causes but maybe you wanna report that to google and see what they say. – ʍѳђઽ૯ท Feb 04 '16 at 16:37
  • I edited the question to include a link to a open issue with Google. Looks like it is a bug with the library – Saurabh Feb 04 '16 at 16:39
  • Yeah, i felt that.Something like my question :) : http://stackoverflow.com/questions/34995278/size-of-shareaction-icon-issue-on-actionbar-with-shareactionprovider-v7 – ʍѳђઽ૯ท Feb 04 '16 at 16:45