【问题标题】:Android First ListView scrolls down to only half of last entry with two ListViewsAndroid First ListView 使用两个 ListView 向下滚动到最后一个条目的一半
【发布时间】:2015-11-29 20:46:10
【问题描述】:

我有我认为可以直接实现的东西:一个带有两个 ListView 的线性布局,两个 ListView 的 layout_weight="1"。第一个是“填充父级”,第二个是“包装内容”。这个想法是,当第二个 ListView 为空时,第一个 ListView 将占据整个屏幕,并且当两个 ListView 都有条目时,屏幕将被共享。好吧,它几乎可以工作,但是当第二个 ListView 为空时,第一个 ListView 仅向下滚动到 LAST 条目的一半。最后一个条目的其余部分不在屏幕上。有谁明白为什么会这样?

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

<LinearLayout
    android:id="@+id/dots"
    android:layout_width="match_parent"
    android:layout_height="24dp" >
</LinearLayout>

<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:src="@drawable/lni_logo" />

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:choiceMode="none"
  android:transcriptMode="alwaysScroll"
  android:layout_weight="1"
  android:id="@android:id/list" >
</ListView>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:choiceMode="none"
  android:transcriptMode="alwaysScroll"
  android:layout_weight="1"
  android:id="@+id/rtsalist">
</ListView>

</LinearLayout>

实际上在这个阶段我还没有为我的第二个 ListView 制作适配器,所以我无法使用其中的条目对其进行测试。但关键是,当它为空时,它应该就像我根本没有 ListView 一样。唉,事实并非如此。当我删除第二个 ListView 时,第一个会正确向下滚动到最后一个元素,显示整个条目。

感谢您的任何见解!

【问题讨论】:

  • fill_parentmatch_parent 是一回事。仅在不推荐使用 fill_parent 时使用 match_parent
  • 您已经为您的 ListView 设置了 layout_weight,但是您需要将这两个 ListView 包装在一个新的 LinearLayot 中方向设置为vertical,并且新的LinearLayout需要将android:weightSum属性设置为2,所以它们各占50%。
  • @Gi0rgi0s 如果其中一个是空的,那一个会占用任何空间吗?我此时所做的是以编程方式将空的设置为消失(rtsaList.setVisibility(View.GONE))。它似乎有效,但我没有尝试过使用条目。当然,当我这样做时,我有第二个列表视图的 layout_height="match_parent"。
  • 您还必须以编程方式将布局权重更改为 2。您这样做的方式是非标准的。
  • 好的;很高兴知道。帮助我更好地理解布局权重。

标签: android listview android-linearlayout


【解决方案1】:

android:paddingBottom="20dp" 之类的行放入您的顶部ListView。调整 20dp 值,直到它看起来像您想要的那样。您可以在列表消失和重新出现时以编程方式更改底部填充,以使其看起来恰到好处。

【讨论】:

  • 我已经这样做了,但不喜欢结果,它并没有真正解决问题;它只会隐藏它。在不同的手机/平板电脑上似乎也有所不同。所以我决定使用 ViewFlipper 并节省宝贵的房地产。在一个 ListView 中,我将拥有测量数据,而在另一个 ListView 中,我将拥有一组实时图表。后者 ListView 不会有大量的图表。但是感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2018-04-09
  • 1970-01-01
  • 2021-06-13
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
相关资源
最近更新 更多