即使在layout_height上使用WRAP_CONTENT,RecycleView也会显示间隙之间的额外空间

问题描述 投票:1回答:1

RecyclerView在布局中显示大空间。

注意:当您开始向下滚动时,项目会正常显示,但是当您返回时,您会开始在项目之间看到大的空白区域。

注意2:所有父项布局都配置了android: layout_height = "wrap_content"。我在这里看到了其他问题的解决方案,但在我的案例中没有解决。

注3:我正在使用CustomCursorAdapter和CustomRecyclerViewAdapter。也许最好看到我的complete repo on github,而不仅仅是这里列出的片段。

完整的代码在这里:

enter image description here


activity_main.xml中

<?xml version="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>

我的知识

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);
    }
}
android android-layout android-recyclerview recyclerview-layout
1个回答
1
投票

我要非常感谢帮助我解决这个问题的@martin_

在列表上设置动画时会发生错误。通过去除

android:animateLayoutChanges="true"

还有这个

.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

从代码应该修复错误。

布局上只需要父级。 This commit应该更好地澄清。

© www.soinside.com 2019 - 2024. All rights reserved.