12

I am having Recyclerview inside Scrollview

 <Scrollview
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

    <LinearLayout
        android:id="@+id/layoutStaticContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

       //Static content.
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp">
           .
           .
        <LinearLayout>

         //Dynamic content(newsfeed)
         <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
 </ScrollView>

Now while scrolling, layoutStaticContent stays fix on the top & recyclerview content scrolls independently in the bottom part. How to scroll the whole content i.e (layoutStaticContent + recyclerview content) such that there is only 1 scrollview? I also tried replacing scrollview with Nestedscrollview but no success.

Rushikesh Talokar
  • 1,515
  • 4
  • 16
  • 32

5 Answers5

13

Use the android.support.v4.widget.NestedScrollView then inside both layout NestedScrollView

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Jignesh Mavani
  • 269
  • 1
  • 7
8

Put the LinearLayout which contains both the static and dynamic data inside of a NestedScrollView and it'll work like a charm.

Here is the code you need:

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/layoutStaticContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   //Static content.
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">
       .
       .
    <LinearLayout>

     //Dynamic content(newsfeed)
     <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
</android.support.v4.widget.NestedScrollView>

I hope it helps!

Farid Arbai
  • 111
  • 2
  • 5
4

One possible way around this is only use RecyclerView with the static content as header to your Recyclerview.

Then the layout would simply be:

//Dynamic content(newsfeed)
     <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

There will be a list_item_header.xml layout for your static content:

//Static content.
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp">
   .
   .
<LinearLayout>

And you'll have to change your recyclerview adapter to contain:

private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;

@Override
public int getItemCount()
{
    int itemCount = super.getItemCount();
    if (mIsHeaderPresent)
    {
        itemCount += 1;
    }
    return itemCount;
}


@Override
public int getItemViewType(int position)
{
    if (mIsHeaderPresent && position == 0)
    {
        return TYPE_HEADER;
    }
    return TYPE_ITEM;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    if (viewType == TYPE_HEADER)
    {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_header, parent, false);
        ViewHolderHeader viewHolder = new ViewHolderHeader(itemView);
        return viewHolder;
    } else if (viewType == TYPE_ITEM)
    {
        return getItemViewHolder(parent);
    }

    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder passedViewHolder)
{
    if (mIsHeaderPresent && passedViewHolder instanceof ViewHolderHeader)
    {
        onBindHeaderViewHolder((ViewHolderHeader) passedViewHolder);
    } else
    {
        onBindItemViewHolder(passedViewHolder);
    }
}
Aniket Shinde
  • 350
  • 3
  • 13
  • Ankit there can be many items in static content & those items can be different on different screens. whereas dynamic content layout is fixed. So I will prefer not to modify adapter because I am using this adapter at many places. – Rushikesh Talokar Jan 06 '16 at 10:12
2

if you want to scroll all data i think you have to use CoordinatorLayout. in CoordinatorLayout you use appbar layout and CollapsingToolbarLayout where you can put your static content. because its a wrong approach in android to use a scroll able container in to another scroll able container. you can use coordinater layout like this.

<?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"
>
<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentScrim="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

    //Static content.
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">
       .
       .
    <LinearLayout>

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

  <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
  />
</android.support.design.widget.CoordinatorLayout>
Ankur1994a
  • 2,112
  • 2
  • 13
  • 18
  • Thanks for your answer. But I don't want Collapsible solution here. Problem with this solution is while scrolling down the static content will start appearing immediately & not as if it is a whole content. – Rushikesh Talokar Jan 06 '16 at 09:40
  • so you want that your static content is look seems also like part of your recycle view. when it reaches its first position only when static content will visible. – Ankur1994a Jan 06 '16 at 09:56
  • i think second answer of @Aniket will help you. – Ankur1994a Jan 06 '16 at 10:00
1

After a lot of searching and trying, I found the solution:

1. Set the height for the RecyclerView inside the ScrollView:

    <ScrollView
       android:id="@+id/scrollView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:fillViewport="true">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    ...
    <android.support.v7.widget.RecyclerView
            android:id="@+id/myPostsRecyclerView"
            android:layout_width="match_parent"
            **android:layout_height="550dp"**
            android:layout_below="@+id/textView7"
            android:background="@drawable/background"
            android:padding="5dp"
            **android:visibility="gone"**
            tools:listitem="@layout/item_send" />
    </RelativeLayout>
    </ScrollView>

2. In the adapter , where you set the data list :

public void setData(List<Post> posts) {
    this.posts.clear();
    this.posts.addAll(posts);
    notifyDataSetChanged();
    if (posts.size() < 1)
        activity.recyclerView.setVisibility(View.GONE);
    else {
        activity.recyclerView.setVisibility(View.VISIBLE);
        ViewGroup.LayoutParams params=activity.recyclerView.getLayoutParams();
        params.height=ViewGroup.LayoutParams.WRAP_CONTENT;
        activity.recyclerView.setLayoutParams(params);
    }

 }
Ester Kaufman
  • 708
  • 10
  • 20