【问题标题】:How to make transparent spaces between groups of items in a ListView?如何在 ListView 中的项目组之间制作透明空间?
【发布时间】:2017-12-20 16:55:21
【问题描述】:

我基本上是将项目分组为两个或一个组,使用 IOS 中的 Table 和它的部分功能很容易做到这一点。我在 android 中使用不同的视图类型来做到这一点,但不得不使用一些技巧来让它工作,因为不知何故,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>

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

我没有问题,它工作得很好,当我不使用这个布局技巧时,只是空白空间会崩溃,需要放一些评论不要忘记它是如何“工作”的。也没有找到类似的问题。

我正在更新 quastion,以便更清楚我在列表视图中实现了什么以及为什么我使用 ViewType 作为项目之间的空间。

想象一下这个列表视图类型结构:

item
item
space
item
item
space
item

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

【问题讨论】:

标签: android android-layout listview


【解决方案1】:

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

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

请参阅this 问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多