【问题标题】:Have a view move down when more elements are added to ListView当更多元素添加到 ListView 时,视图会向下移动
【发布时间】:2014-09-06 11:54:33
【问题描述】:

你如何让ListView 填满屏幕并同时移动下面的视图直到它到达底部?例如,对于下面的图像,我希望“创建”按钮在添加新元素时向下移动屏幕,然后当按钮到达底部时,ListView 开始滚动(创建按钮仍然出现在屏幕)。

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_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"
        >

    <ListView
            android:id="@+id/galleryList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:layout_alignParentTop="true"
            />

    <LinearLayout
            android:id="@+id/lowerButtonLayout"
            android:layout_width="fill_parent"
            android:layout_height="60dp"
            android:background="#ffa8269f"
            android:layout_alignParentBottom="true"
            xmlns:android="http://schemas.android.com/apk/res/android">

        <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@color/button_button_bg_colour"
                android:onClick="addRow"
                android:text="Create"/>
    </LinearLayout>

</RelativeLayout>

【问题讨论】:

  • 创建自定义 xml 并将其添加为 ListView 的页脚。并且您必须将 ListView 高度设置为 fill_parent。
  • @dpsingh - 希望这可以完全通过布局来完成
  • 另一种方法是在 ScrollView 中使用 ListView。
  • @dpsingh - 我认为它会起作用,但没有
  • 好吧,我举个例子

标签: android android-layout android-linearlayout android-relativelayout


【解决方案1】:

很遗憾,只用xml文件是做不到的。

最简单的方法是使用 2 个按钮。一个在您的主布局中,一个在您的ListView 中(例如作为页脚视图)。然后根据ListView是否达到特定高度,只需将一个按钮的可见性设置为View.GONE即可。 如果您的单元格具有相同的高度,则计算非常简单。

不要在ScrollView 中使用ListView。它将破坏ListView 的所有目的(能够回收细胞)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 2021-04-29
    相关资源
    最近更新 更多