Android ListView的高度,以包装到单个列表项的高度 [英] Android ListView height to wrap to the height of one single list item

查看:84
本文介绍了Android ListView的高度,以包装到单个列表项的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a view that contains "two" ListViews. The first one's height should be equal to a single list item height, and the second ListView's height should take up the remaining space.

有什么想法吗?

<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这个!如果您不知道答案,那么只需移至另一个即可!

Why people are -1 this!! If you don't know the answer then just simply move to another!

推荐答案

虽然我同意OP中的代码段对问题没有任何帮助,但是问题本身非常合理,我必须在我的工作中做同样的事情项目.

While I agree that the code snippet in the OP contributes nothing to the question, the question itself is very reasonable and I had to do exactly the same thing in my project.

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

The way this can be done (assuming all items in the list have the same height) is by measuring an item once and assigning its height to the listview. Here is what worked for me (adapter's code):

    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;
    }

这篇关于Android ListView的高度,以包装到单个列表项的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆