3

i have recycleview and in top i have one view which includes textview .

my problem is when scroll up that text-view always at top and only recycle-view scroll .. is any way i can scroll both . i don't want to use view type in recycle-view as it already use for some other purpose

my XML

 <Rel layout>
 <text-view id="text"/>
 <recycle-view below="text" />

so how textview will go up and down with recycle-view scroll can any give small snippet for this

Does
  • 31
  • 1
  • 3

1 Answers1

6

If you really want to keep the TextView separate from the RecyclerView, wrap them in a NestedScrollView instead:

<NestedScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
      <TextView />
      <RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
  </LinearLayout>
</NestedScrollView>

When you set up your RecyclerView, you will probably want to call RecyclerView#setNestedScrollingEnabled(false) otherwise the RV may consume some scrolling inside the parent (which you don't want).

NOTE: This approach makes the tradeoff that the RV will be forced to layout all of its views, losing the recycling benefit. The correct approach would be to properly dedicate a viewType to the RV for this header TextView and not deal with wrapping it inside additional ViewGroups.

ekchang
  • 939
  • 5
  • 11
  • even this remain at top . my this view is subvivew of parsent view which have coridnt layout with collasping tollbar – Does May 04 '16 at 02:54
  • Edited. The NSV should have a single child. It's actually important to know you were trying to do this with CoordinatorLayout because regular ScrollView does not work at all inside a CoordinatorLayout, only NSV does. – ekchang May 04 '16 at 03:08
  • with this my RecyclerView is not scrollable i have to five fix heigth – andro May 04 '16 at 12:17
  • You need to fully explain the context of the problem and desired solution. Include all of the relevant viewgroups used/nested because they will of course affect how the final UI behaves. The original question did not mention CoordinatorLayout etc, so I believe this answer technically satisfies the question unless the question is edited to be more specific. – ekchang May 04 '16 at 18:31
  • In case of nestedscrollview you'll have to layout all of the views inside RV, so all benefits from RV (recycling) are gone. – dilix Apr 04 '17 at 12:18
  • 1
    @dilix this is true, but the answer addresses the OP's problem with the tradeoff that RV will layout all of the RV views. If that is unacceptable, then the correct approach is to allocate a dedicated `viewType` and put it in the RV. I'll edit the answer to note this tradeoff. – ekchang May 05 '17 at 16:58