【问题标题】:RecyclerView in CoordinatorLayout shows items only after scrolling downCoordinatorLayout 中的 RecyclerView 仅在向下滚动后显示项目
【发布时间】:2023-04-03 07:50:01
【问题描述】:

我在 CoordinaterLayout 中使用 RecyclerView 来显示搜索结果项。然而,这些项目只有在滚动一点后才会显示,我不知道为什么。我认为这与 RecyclerView 没有调整到 CoordinatorLayout 中减小的大小有关。 有什么线索吗?

布局非常简单,是android studio提供的基本模板,除了RecyclerView。

activity_results_recycler.xml:

<?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"
    tools:context="com.alamgir_swi.abjad.ResultsRecyclerActivity">

    <android.support.design.widget.AppBarLayout android:id="@+id/app_bar"
        android:fitsSystemWindows="true" android:layout_height="@dimen/app_bar_height"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout"
            android:fitsSystemWindows="true" android:layout_width="match_parent"
            android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary">

            <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
                android:layout_height="?attr/actionBarSize" android:layout_width="match_parent"
                app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_results_recycler" />

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin" app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end" android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

content_results_recycler.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_results_recycler"
    tools:context="com.alamgir_swi.abjad.ResultsRecyclerActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/abjadList"
    >
</android.support.v7.widget.RecyclerView>

abjad_entry.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/abjad_entry"
        android:layout_gravity="right" />
</RelativeLayout>

感谢您的任何提示。

【问题讨论】:

  • 在 content_results_recycler.xml 中使用 android:fitsSystemWindows="true"。看看它是否有效

标签: android android-recyclerview vertical-scrolling android-coordinatorlayout


【解决方案1】:

找到问题的根源。

从 CursorLoader 获取数据后,我需要在我的 RecyclerView.LayoutManager 实例上调用 notifyDataSetChanged()。

【讨论】:

    猜你喜欢
    • 2019-06-15
    • 2015-06-10
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    相关资源
    最近更新 更多