内容占位符如何在Android RecyclerView中实现,就像在Facebook中一样?

时间:2016-07-12 06:26:46

标签: android android-recyclerview

我需要TextViewRecyclerView的内容占位符,直到获取数据&从服务器解析。

通过内容占位符,我不是指默认文字或说没有结果的文字。请查看Facebook在显示网络上的实际数据之前如何显示灰色动画块。我想实现这一点。

基本上是一种装载机,除非我的数据准备好显示(请不要建议我使用水平或旋转装载机)。 例如:我显示一个灰色的块,一旦我的数据准备好显示,我将删除该块,然后显示文本。

所有这些都需要在RecyclerView中完成。我希望我能清楚自己的需要。

我如何实现这一目标?

  1. 我无法在RecyclerView中放置虚拟视图,因为除非我有数据,否则getCount将返回0,因此不会显示任何视图。

    < / LI>
  2. 我不想显示虚拟布局,然后在获取实际数据时将其删除,这看起来并不干净。

3 个答案:

答案 0 :(得分:0)

这应该有用,

<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No Results" />

id 可以解决问题!

你必须使用ListViewActivity才能工作!

如果没有扩展ListViewActivity

试试,

ListView.setEmptyView()read here

答案 1 :(得分:0)

在不知道Facebook的实施情况的情况下,我假设他们使用&#34; lean&#34;来加载初始数据源。根据您可以创建占位符(可能是RecyclerView中的类型)的数据,您可以从中了解需要显示的单位数量,一旦加载了该占位符的完整数据,您就会通知数据设置更改并将占位符替换为完整项目(RecyclerView中的另一种类型)。

  

除非我的数据准备好显示,否则基本上是一种加载器

如果你绝对没有数据,例如你没有数量的&#34;块&#34;如果您从数据源返回一个空数据集,则显示一个占位符,因为它可能会被丢弃。

答案 2 :(得分:0)

您需要创建骨架布局并将其在整个屏幕上充气。然后,您可以使用其他库来添加闪烁效果。

drawable / skeleton.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <solid android:color="@color/skeleton"/>
<corners android:radius="4dp"/>
</shape>

layout / skeleton_row_layout.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"
  android:layout_width="match_parent"
  android:layout_height="@dimen/row_layout_height">

  <View
    android:id="@+id/icon"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_margin="15dp"
    android:layout_gravity="center_vertical"
    android:background="@drawable/skeleton"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

  <View
    android:id="@+id/topText"
    android:layout_width="200dp"
    app:layout_constraintTop_toTopOf="@id/icon"
    app:layout_constraintStart_toEndOf="@id/icon"
    android:layout_height="15dp"
    android:layout_marginLeft="16dp"
    android:background="@drawable/skeleton"/>

  <View
    android:id="@+id/bottomText"
    android:layout_width="250dp"
    android:layout_height="15dp"
    app:layout_constraintTop_toBottomOf="@id/topText"
    android:layout_marginTop="10dp"
    app:layout_constraintStart_toEndOf="@id/icon"
    android:layout_marginLeft="16dp"
    android:background="@drawable/skeleton"/>

  <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@drawable/skeleton"
    app:layout_constraintTop_toBottomOf="@id/icon"
    android:layout_marginTop="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>

</android.support.constraint.ConstraintLayout>

在我写的本教程中,您将找到如何用recyclerView替换骨架:https://medium.com/@sha17187/upgrade-progress-loading-with-a-skeleton-and-shimmer-effect-in-android-863ea4ff5b0b