【问题标题】:How to make a GridView of ListViews with dynamic size如何制作具有动态大小的 ListView 的 GridView
【发布时间】:2014-09-05 06:19:42
【问题描述】:

通常我会尝试重新生成此布局:

这个想法是你有父类别(每个列表中的顶部项目),然后是它的子类别列表。我从服务器获取此信息,所以我不能硬核任何东西。没有在代码中修改任何布局,目前我只是想让布局工作,然后我会将其连接到实际数据。 片段布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.<redacted>.app.EditCategoriesActivity.EditCategoriesFragment">

<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="@dimen/edit_categories_list_width"
    android:id="@+id/edit_categories_grid"
    android:gravity="center_horizontal"
    android:numColumns="auto_fit"
    android:stretchMode="spacingWidthUniform"/>

</RelativeLayout>

GridView 项目视图布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/border_box"
android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test Cat"
    android:layout_weight="1"
    android:id="@+id/edit_categories_grid_item_parent_name"
    android:layout_margin="10dp"/>
<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:paddingEnd="30dp"
    android:paddingRight="30dp"
    android:id="@+id/edit_categories_grid_item_parent_switch"/>
</LinearLayout>
<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:minHeight="?android:attr/listPreferredItemHeightLarge"
    android:layout_below="@id/edit_categories_grid_item_parent_name"
    android:id="@+id/edit_categories_grid_item_list"
    android:layout_margin="10dp"
    android:paddingStart="30dp"
    android:paddingLeft="30dp"
    android:clickable="true"
    android:choiceMode="singleChoice"
    />

</LinearLayout>

ListView 项目布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:layout_gravity="center_vertical">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:text="Child"
    android:id="@+id/edit_categories_list_item_title"/>
<Switch
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:gravity="center_vertical|end"
    android:id="@+id/edit_categories_list_item_switch"/>

</RelativeLayout>

目前它看起来像这样:

测试数据的结构是:

测试1>

测试儿童猫

测试儿童猫 2

-

测试2>

TestChild 猫 -2

TestChild Cat 2 -2

TestChild Cat 3 -2

TestChild Cat 4 -2

TestChild Cat 5 -2

TestChild Cat 6 -2

TestChild Cat 7 -2

TestChild Cat 8 -2

- 列表视图也很难滚动......我的意思是我已经尝试在我的测试中将它们滚动到“命中区域”超过一百次,并且只得到 1(如果重要的话,Test2 一个)到滚动一次...是的...

理想情况下,所有列表项都将显示,并且在 gridview 项中不会滚动,只有 gridview 本身。

如果我可以提供任何您认为对您有帮助的信息,请告诉我。就像我说的那样,我基本上只是将数据绑定到代码中的视图,仅此而已,这样我就可以看到这将如何影响布局……除了数据本身的结构之外,您还知道。 >.>

无论如何,感谢您的宝贵时间!

【问题讨论】:

  • 嵌套滚动视图在 Android 中不能很好地工作,如果有的话。您可能想要使用 GridLayout 而不是 GridView。
  • 感谢您的意见!现在就去试试吧!
  • 是的。这将比使用 ListViews 多一点工作,但不会有滚动问题。
  • 太棒了!我很高兴听到这个消息,但我不应该仅仅因为一个模糊的轮廓而得到任何代表。你做了所有繁重的工作!不过,我会要求您在可能的情况下发布并接受答案,这样这个问题就不会继续显示为未回答。你不会从你自己那里得到任何分数,但是给我们一个很好的解释(如果有必要,也许一两个sn-p),你会得到我的支持!谢谢。我很感激这个提议。
  • 好吧,无论哪种方式都感谢您的帮助!有时间我会尽快贴出详细答案^^;

标签: android android-layout gridview android-listview android-gridview


【解决方案1】:

终于得到了几乎完全符合我想象的东西!

我第一次尝试更改是删除列表视图并将它们替换为具有垂直方向的LinearLayouts,然后循环遍历数据以填充它并为数据膨胀所有视图。这有点工作,但GridView 的“单元格”会相互重叠,并且在您滚动时会重新定位......不用说这并不理想。

然后我再次查看GridLayout 并决定尝试使用与LinearLayouts 基本相同的方式填充它,它几乎可以完美运行!把它扔进ScrollView,它就可以工作了!现在单元格没有按照我想要的方式排列,我必须为不同的设备提供多个布局文件,因为它没有用于列的auto-fit 选项,但总的来说我对结果非常满意!此外,另一个明显的缺点是它不再被回收,但我知道我不会有大量数据并且加载的图像为 0,所以我很幸运,我不必拥有它。希望这对其他人有帮助!

附:一旦 RecycleView 完全发布并稳定,它可能就是要走的路,所以如果你还没有看过它,做! ^_^

【讨论】:

    猜你喜欢
    • 2015-10-29
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多