AndyRoid
AndyRoid

Reputation: 5067

Slide RecyclerView up as you swipe it up, or slide recyclerview down as you swipe down

I want to create a Fragment with a RecyclerView that slides up and shows more items as you slide it up.

Here is an example of what I am talking about.

Initial Creation:

enter image description here

User Swipes Up to slide RecyclerView up, shows more items:

enter image description here

There are a few issues, I would like to not use a CoordinatorLayout, and I would like to set it to where the items in the RecyclerView stack up directly on top of the EditText.

This is the layout code I am using:

<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

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

        <View
            android:id="@+id/emptyView"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:background="@android:color/transparent"
            app:layout_collapseMode="parallax"/>

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

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <EditText
            android:id="@+id/editText"
            android:paddingLeft="16dp"
            android:inputType="textAutoCorrect"
            android:layout_centerVertical="true"
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="56dp"/>

    </RelativeLayout>

</RelativeLayout>

And I get something like this:

enter image description here

This is definitely not scalable, and empty view would need to be consistently measured.

Upvotes: 17

Views: 1725

Answers (3)

AndyRoid
AndyRoid

Reputation: 5067

I solved this by using a custom TouchListener all of the other solutions were very basic and limited to a specific and boxed use-case.

The way I did this was to implement a new TouchListener based off of this library:

BounceTouchListener

I get the following results:

enter image description here

Upvotes: 11

Alper Cem Polat
Alper Cem Polat

Reputation: 154

You can use setReverseLayout of LinearLayoutManager

Used to reverse item traversal and layout order. This behaves similar to the layout change for RTL views. When set to true, first item is laid out at the end of the UI, second item is laid out before it etc. For horizontal layouts, it depends on the layout direction. When set to true, If RecyclerView is LTR, than it will layout from RTL, if RecyclerView} is RTL, it will layout from LTR. If you are looking for the exact same behavior of setStackFromBottom(boolean), use setStackFromEnd(boolean)

Upvotes: 0

David
David

Reputation: 37596

Use RecyclerView with 2 types of rows:

  1. Empty row/rows colored gray (without divider)
  2. Rows with content as you already have

The RecyclerView will have to match_parent to the entire fragment

So you will get the right effect of a "blank" area on top of the RecyclerView.

enter image description here

Upvotes: 5

Related Questions