android - 如何在 ListView 中的项目组之间创建透明空间?

标签 android android-layout listview

我基本上是将项目分组为两个或一个组,使用 IOS 中的表及其部分功能可以轻松实现。我在 android 中使用不同的 View 类型来做到这一点,但不得不使用一些 hack 来让它工作,因为不知何故 Android 没有使用空间项目布局的布局高度。我通过创建 R.layout.list_section_space 来修复它,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#600d47a1">

<View
    android:layout_width="match_parent"
    android:layout_height="25dp" />
</FrameLayout>

但是当这样指定的时候

<?xml version="1.0" encoding="utf-8"?>
<View
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#600d47a1"
    android:layout_width="match_parent"
    android:layout_height="25dp" />
</FrameLayout>

我认为它是折叠的什么的,而且不可见..

我没有遇到任何问题,它运行良好,只是当我不使用此布局 hack 时空白空间会崩溃并且需要在此处添加一些评论以不要忘记它是如何“工作”的。也没有发现类似的问题。

我正在更新 quastion 以便更清楚我在我的 ListView 中取得的成就以及为什么我在项目之间使用 ViewType 作为空间。

想象一下这个 ListView 类型结构:

item
item
space
item
item
space
item

所以我有两个 View ,一个是item,一个是space。每个都有其 View 类型,并且空间 View 布局会折叠,因为如果我不使用我的解决方法,ListView 显然会忽略顶部 layout_height 参数。我看不到使用 DividerItemDecoration 的想法。

最佳答案

在不知道您的适配器的实现的情况下,我只能猜测(至少部分)问题在于您扩充列表行的方式。确保给它们充气,使用

getLayoutInflater().inflate(R.layout.list_item, parent, false) 其中 parent 是 getView() 中提供的 AdapterView > 适配器的方法。

参见 this问题。

关于android - 如何在 ListView 中的项目组之间创建透明空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47910912/

相关文章:

android - 设置 Listview 的项目高度不起作用

android - 这个简单的 android listView 有什么问题?

android - onReceive 在 BroadcastReceiver 中为 android 中的网络监听器调用了两次

javascript - Firebase 函数从 Firebase DB 获取数据以发出推送通知

java - ProGuard 导致应用程序延迟

android - 应用程序正在运行但 MAP 是空白的我正在使用 google map api 来定位某个位置

android - 使用滚动按钮时应用程序崩溃

android - MultiAutoCompleteTextView 和 AutoCompleteTextView 的区别

android - 如何在 Android 中水平等距显示 2 行中的 6 个项目

listview - react native : ListView item does not scale full width