9

I realize the following is incorrect but I suppose you can treat it as pseudocode. I'm wondering, how can I "attach" a whole layout to my RecyclerView so that when I scroll down the layout scrolls out of sight along with it. Presently the TextView above the RecyclerView is fixed on the screen, whereas all the data in my RecyclerView is scrollable. I want the TextView to move out of sight as the user scrolls down the page as well. If you look at Instagram, all the user profile information at the top of your gallery disappears as you scroll down the page. How can I do this? How can I add the TextView programatically?

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello!" />

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="?android:attr/actionBarSize"
    android:padding="@dimen/item_margin"
    android:id="@+id/recycler_view"
    android:clipToPadding="false">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello!" />

</android.support.v7.widget.RecyclerView>
Martin Erlic
  • 5,467
  • 22
  • 81
  • 153

4 Answers4

7

You can try Android-ObservableScrollView.

Refer following link: https://github.com/ksoichiro/Android-ObservableScrollView

Rahul Lad
  • 411
  • 4
  • 10
6

You can add that TextView as one of the RecyclerView's items using a custom RecyclerAdapter or you could use a ScrollView with LinearLayout as its content, instead of a RecyclerView as it's not advisable to use one inside a ScrollView.

Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22
  • I see... I really don't want to change from RecyclerView as I'm already deeply entrenched in it and it seems more performant for my purposes. I suppose I'll look into adding the TextView programatically. – Martin Erlic Dec 23 '15 at 04:29
  • @bluemunch that would definitely be much efficient. – Kyle Emmanuel Dec 23 '15 at 04:31
6

I did the same using these two great resources:                                                        

https://plus.google.com/114432517923423045208/posts/EdkQFx1RqhE

https://gist.github.com/gabrielemariotti/4c189fb1124df4556058

                     

Gal
  • 197
  • 1
  • 12
Javad Jafari
  • 710
  • 6
  • 11
0

One possible solution: You could treat your TextView as one of the RecyclerView's items but it will have different appearance.

 @Override
public int getItemViewType(int position) {
    if (position == 0) {
        return HEADER;
    } else {
        return NORMAL_ITEM;
    }
}

@Override
public StandardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == HEADER) {
        //inflate your textview layout header. Create a viewholder                  //for it
    } else {
        // inflate normal layour
    }
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    if (getItemViewType(position) == HEADER) {
        // BIND header View
    } else {
        //BIND NormalView
    }
}
Tin Tran
  • 2,265
  • 1
  • 14
  • 17