7

I want to place a RecylerView inside NestedScrollView as below

activity_service_menu.xml

<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HELLO" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp" />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

ServiceMenuActivity.java

public class ServiceMenuTActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_menu_t);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        rv.setHasFixedSize(true);
        rv.setAdapter(new RvAdapter());
    }

    private static class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvHolder> {

        @Override
        public RvHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View serviceMenuItemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_service_menu, parent, false);
            return new RvHolder(serviceMenuItemView);
        }

        @Override
        public void onBindViewHolder(RvHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 100;
        }

        public static class RvHolder extends RecyclerView.ViewHolder {

            public RvHolder(View itemView) {
                super(itemView);
            }
        }
    }

}

I have put the linearLayout inside scrollView and nestedScrollView. But the RecyclerView is not visible. If I replace ScrollView with FrameLayout or any other layout, then RecyclerView is visible.

I want to use nestedScrollView and scroll the total layout when recyclerView is scrolled. Unfortunately recyclerView is not even visible.

Narendra
  • 97
  • 1
  • 5
  • do you really need `LinearLayout` as parent of `RecyclerView` inside `NestedScrollView` ? – Vipul Asri Oct 14 '15 at 08:25
  • No. As the ScrollView accepts only 1 child, I have put LinearLayout as wrapper. I need RecyclerView below the TextView and when RecyclerView is scrolled, the whole view should scroll. – Narendra Oct 14 '15 at 14:08
  • can you describe basically what type of behavior you want? like Collapsing Toolbar or anything else. – Vipul Asri Oct 15 '15 at 05:55
  • Not sure it would work but try setting a fix height to recyclerView instead of wrap_content – Apurva Oct 15 '15 at 09:01
  • @Apurva: Fixing the height of recyclerView worked. For people who come to this question.. more info on using nestedScrollView inside scrollView: [link](http://stackoverflow.com/questions/31000081/how-to-use-recyclerview-inside-nestedscrollview) – Narendra Oct 15 '15 at 10:17

2 Answers2

13

Follow this sample will get idea where you done mistake. Main Line is : android:fillViewport="true".

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:theme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="24dp">


        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">


        <com.app.view.CustomRecyclerView
            android:id="@+id/recycler_movie_suggestion"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.v7.widget.CardView>

    </LinearLayout>

    </android.support.v4.widget.NestedScrollView>
Karthik Pugal
  • 356
  • 4
  • 7
  • 2
    You sir are amazing. It seems setting fillViewport="true" on the NestedScrollView did the trick. Thank you! – Pkmmte Oct 29 '15 at 00:16
  • This is the only one solution that worked for me, i tried each one from related questions but no one worked. – UrielUVD Feb 17 '16 at 23:19
0

when you use two scrollble element inside each other you are in hot water! you have to calculate the Recycler item height and then find the whole recycler height. look at below link, I explain completely this problem.

Use RecyclerView insdie ScrollView with flexible recycler item height

I hope it help you

Community
  • 1
  • 1
Ashkan
  • 1,122
  • 3
  • 12
  • 36