RecyclerView is showing large white space in the layout.
Note: When you start scrolling down, the items appear normally, but when you go back up, you start to see large blanks between the items.
Note2: All parent layouts are configured with android:layout_height="wrap_content"
. I saw this solution in SOen, but it did not resolve in my case.
Note3: I'm using a CustomCursorAdapter along with a CustomRecyclerViewAdapter. It might be better to see my complete repository than just the snippets of code listed here.
The complete code is here:
- link
- BUG Demo:
activity_main.xml
<?xmlversion="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="wrap_content"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
card_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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:id="@+id/card_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:animateLayoutChanges="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="4dp">
<android.support.constraint.ConstraintLayout
android:id="@+id/constraint_card_child"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true">
<android.support.constraint.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="16dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="16dp" />
<ImageView
android:id="@+id/test_image"
android:layout_width="0dp"
android:layout_height="192dp"
android:layout_marginTop="72dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/params_card_subtitle"
android:layout_width="wrap_content"
android:layout_height="17dp"
android:layout_marginTop="16dp"
android:text="Subtitle goes here"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/test_image" />
<TableLayout
android:id="@+id/card_tableLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/params_card_subtitle">
<TableRow
android:id="@+id/item1_row"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/test_table_item1"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="item1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/item1_table_indicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
<TextView
android:id="@+id/test_table_item1_value_last"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="0.722" />
<TextView
android:id="@+id/test_table_item1_value_lastest"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="0.722" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp">
<TextView
android:id="@+id/test_table_item2"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="item2" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/item2_table_indicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
<TextView
android:id="@+id/test_table_item2_value_last"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="0.001" />
<TextView
android:id="@+id/test_table_item2_value_lastest"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="0.001" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp">
<TextView
android:id="@+id/test_table_item3"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="item3" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/item3_table_indicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
<TextView
android:id="@+id/test_table_item3_value_last"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="0.000" />
<TextView
android:id="@+id/test_table_item3_value_lastest"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="0.000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp">
<TextView
android:id="@+id/test_table_item4"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="item4" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/item4_table_indicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
<TextView
android:id="@+id/test_table_item4_value_last"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="0.000" />
<TextView
android:id="@+id/test_table_item4_value_lastest"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="0.000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp">
<TextView
android:id="@+id/test_table_item5"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="item5" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/temp_table_indicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
<TextView
android:id="@+id/test_table_item5_value_last"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="26 °C" />
<TextView
android:id="@+id/test_table_item5_value_lastest"
style="@style/TableText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="26 °C" />
</TableRow>
</TableLayout>
<View
android:id="@+id/divider3"
android:layout_width="368dp"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="@+id/expand_collapse_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/card_tableLayout"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/expand_collapse_button"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expand"
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:fontFamily="sans-serif"
android:padding="0dp"
android:text="Title"
android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textSize="20dp"
android:typeface="sans"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline4" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:fontFamily="@font/roboto"
android:padding="0dp"
android:text="Subtitle goes here"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textSize="14dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<android.support.constraint.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="16dp" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
MainActivity
public class MainActivity extends AppCompatActivity
implements LoaderManager.LoaderCallbacks<Cursor> {
private final int LOADER_ID = 0;
private DbHelper helper;
private SQLiteDatabase db;
private RecyclerView recyclerView;
private ListCursorAdapter cursorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Initialize */
cursorAdapter = new ListCursorAdapter(this, null);
helper = new DbHelper(this);
/* Setup Recycler */
recyclerView = findViewById(R.id.recycler);
recyclerView.setAdapter(cursorAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
getLoaderManager().initLoader(LOADER_ID, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
db = helper.getWritableDatabase();
/* Create dummy data to test */
int i = 0;
while (i < 10){
ContentValues values = new ContentValues();
values.put(NAME_COLUMN, String.valueOf(i));
db.insert(TEST_TABLE, null, values);
values.clear();
i++;
}
return new CustomCursorLoader(this);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
cursorAdapter.swapCursor(cursor);
cursorAdapter = new ListCursorAdapter(this, cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
cursorAdapter.swapCursor(null);
}
}