如何使的LinearLayout滚动两个列表视图滚动用于包装的内容 [英] How to make Linearlayout scrollable with two listviews scrollable which wrap their content

查看:141
本文介绍了如何使的LinearLayout滚动两个列表视图滚动用于包装的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经被问了很多次,但我仍然还没有找到解决办法我真的很喜欢。有人可以请建议最优雅的方式做我想要做的:

I know this has been asked many times but I still have yet to find a solution I really like. Can someone please suggest the most elegant way to do what I'm trying to do:

我有我现在有一个的LinearLayout(垂直)两个列表视图。我希望以列表视图,当涉及到的高度,使他们永远需要在内部滚动换他们的内容。相反,我想为了看到任何溢出在页面上向下滚动。

I have two Listviews which I currently have in a LinearLayout (vertical). I want the Listviews to wrap their content when it comes to height so they never need to scroll internally. Instead, I would like to scroll down on the page in order to see anything that overflows.

在此先感谢您的帮助!如果你完全建议重新做架构,然后我开到这一点。我的唯一要求是一个解决方案是干净和简单越好。 hackish的解决方法不是preferred。 :)

Thanks in advance for the help! If you suggest completely re-doing the architecture then I'm open to that as well. The only thing I ask is for a solution to be as clean and simple as possible. Hackish workarounds not preferred. :)

推荐答案

据我所知,你正在试图使的 ListView的身高作为其内容要求。

As far as I know, you are trying to make the ListView as height as its content requires.

您可以创建延伸的ListView 自定义列表视图并覆盖其onMeasure方法是这样的:

You can create a custom listview which extends ListView and override its onMeasure method like this:

public class UnscrollableListView extends ListView {

    public UnscrollableListView(Context context) {
        super(context);
    }

    public UnscrollableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public UnscrollableListView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int maxHeightSpec = MeasureSpec.makeMeasureSpec( 
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
        super.onMeasure(widthMeasureSpec, maxHeightSpec); 
    }

}

这会使你的列表视图来包装他们的内容。

This will make your listview to wrap their content.

当你需要向下滚动,尝试添加滚动型在布局换行的LinearLayout
最后,你的布局是一样的东西:

As you need to scroll down, try to add a ScrollView in your layout to wrap the LinearLayout. Finally, your layout is something like:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <com.example.UnscrollableListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <com.example.UnscrollableListView
            android:id="@+id/listview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</ScrollView>

是不是你想要的?

Is it what you want?

但我不得不说,这些codeS使列表视图来,就像有很多类似孩子的垂直的LinearLayout执行。
它不能采取回收意见的优势来提高性能的布局,因为没有视图将被回收。

But I have to say that these codes make listview to perform just like a vertical linearlayout with a lot of similar children. It could not take advantage of the recycling views to improve layout performance because no view is going to be recycled.

您可以对这篇文章一起来看看:
性能提示为Android的ListView的

You can have a look on this article: Performance Tips for Android’s ListView

这篇关于如何使的LinearLayout滚动两个列表视图滚动用于包装的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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