【问题标题】:Android ListView height to wrap to the height of one single list itemAndroid ListView 高度换行到单个列表项的高度
【发布时间】:2015-03-01 20:04:45
【问题描述】:

我正在尝试创建一个包含“两个”ListViews 的视图。 第一个的高度应该等于单个列表项的高度,第二个ListView的高度应该占据剩余空间。

有什么想法吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="vertical" >

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_forecast"
    android:name=MyFragment"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    tools:layout="@android:layout/list_content" />

<FrameLayout
    android:id="@+id/fragment_wear"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

为什么人们是-1这个!!如果你不知道答案,那就直接换一个吧!

【问题讨论】:

  • 列表项高度是否可变?
  • @djechelon 更新了我的代码。100dp 是我的问题,我希望它是动态的。
  • 如何填充列表?
  • 人们对此表示反对,因为您没有说明您为尝试自己解决问题所做的工作。您添加的信息似乎不相关(您添加的 XML 中没有 ListView)并且很难回答您的问题。 re: 怎么计算my_list_item_height,我问你高度是不是可变的,你说是固定的; 这个固定高度是你应该使用的my_list_item_height
  • 如果第一个ListView中所有项目的高度都相同,但由于屏幕分辨率不同,无法提前确定,那么我不认为可以在 xml 中指定 ListView 本身的高度应该等于项目的高度(我可能错了)。您可以做的是在运行时对项目调用layout()getMeasuredHeight(),并在运行时设置顶部ListView 的高度。所有其他信息,例如第二个 ListView 在第一个之下的事实都可以放在 xml 中。

标签: java android listview layout


【解决方案1】:

真正的问题是,您希望通过仅显示 1 个项目的 ListView 获得什么好处 - 为什么不在标准视图中显示该项目?


如果你有一个固定高度的ListItem,你可以把它设置为android:layout_height属性:

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

  <ListView
    android:id="@+id/top_list_view"
    android:layout_width="match_parent"
    android:layout_height="?android:listPreferredItemHeight"
    android:background="@android:color/holo_green_dark" />

  <ListView
    android:id="@+id/bottom_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright" />
</LinearLayout>

【讨论】:

  • 有没有类似'?android:listPreferredItemHeight'的东西可以实际得到计算出来的列表项高度?
  • 不,这是对静态值的引用,只是在运行时根据您活动的主题解决。
  • 回答您的问题:顶部 ListView 是一个自定义的,水平旋转而不是垂直旋转。但它扩展了 ListView,因为我需要它的所有功能。到目前为止,我无法将其包装到一个列表项。
  • 这听起来很像一个 XY 问题,我会结束这个问题并问另一个说明你需要什么,而不是说明你需要什么来实现你的认为您需要 - 即 ListView 提供您想要使用的功能。
  • 我需要的是问题主题中的内容:Android ListView height to wrap to the height to one list item..
【解决方案2】:

虽然我同意 OP 中的代码 sn-p 对问题没有任何贡献,但问题本身非常合理,我必须在我的项目中做完全相同的事情。

可以做到这一点的方法(假设列表中的所有项目具有相同的高度)是通过测量一个项目并将其高度分配给列表视图。这对我有用(适配器的代码):

    private int cellHeight = -1;
    ...

    public override View getView(int position, View convertView, ViewGroup parent)
    {
        View view = context.LayoutInflater.Inflate(R.layout.list_item, null);
        ...

        if (cellHeight == -1)
        {
            // Measure the cell once and set this height on the list view. Won't work correctly if 
            // cells have different heights
            int UNBOUNDED = MeasureSpec.MakeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            view.Measure(UNBOUNDED, UNBOUNDED);
            cellHeight = view.getMeasuredHeight();
            LayoutParameters para = listview.getLayoutParams();
            para.Height = cellHeight;
            listview.setLayoutParams(para);
        }

        return view;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多