Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView height wrap_content calculation is incorrect

Tags:

I want my RecyclerView to wrap_content. I don't want any scrolling inside RecyclerView, it should adjust to the height of inner children. I wan't my parent ScrollView to scroll the content of my activity.

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <!-- scrolls a little bit as RecyclerView goes slightly down beyond the screen -->
  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
      <!-- still scrolls inside -->
      <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

Populate RecyclerView:

myAdapter = new MyAdapter();
layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(myAdapter);

I use RecyclerView library where wrap_content issues should be fixed:

dependencies {
    compile 'com.android.support:recyclerview-v7:25.0.0'
}

enter image description here

Basically RecyclerView height calculation doesn't work well for me here. RecyclerView still has it's own scroll and ScrollView scrolls a little as well. If I try to set some rediculous RecyclerView height to 1000dp so that it's bigger than height of items in total, scrolls work as needed e.g. RecyclerView doesn't scroll and ScrollView scrolls the activity with all RecyclerView items.

So what did I do wrong? :)

like image 469
Andrei Avatar asked Nov 13 '16 14:11

Andrei


1 Answers

All I needed is to use android.support.v4.widget.NestedScrollView instead of a ScrollView.

like image 110
Andrei Avatar answered Sep 25 '22 16:09

Andrei