里面滚动型图像的网格 [英] Grid of images inside ScrollView

查看:168
本文介绍了里面滚动型图像的网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个文本和图像的屏幕。我想要的图像进行布局像一个网格,如下图所示,但我希望他们有没有其他的一个提供周边滚动型的滚动功能。

I'm trying to create a screen with both text and images. I want the images to be laid out like a grid, as shown below, but I want them to have no scroll functionality other that the one provided by the surrounding ScrollView.

这是形象最能说明我的问题:

An image will best illustrate my question:

<ScrollView>
    <LinearLayout>
        <ImageView />
        <TextView />
        <GridView />
        <TextView />
    </LinearLayout>
</ScrollView>

什么是要表现出不同数量的图​​像,在电网没有滚动功能的网格中的最佳方法是什么?

What is the best way to make show a grid of a varying number of images, where the grid does not have scroll functionality?

请注意,禁用了GridView的滚动功能不起作用,因为这只是禁用滚动条,但不显示所有项目。

Please note that disabling the scroll functionality for the GridView does not work, as this just disables the scrollbars but does not show all items.

更新: 下面的图片是什么样子的滚动条在GridView禁用。

Update: The image below shows what it looks like with scrollbars disabled in the GridView.

推荐答案

哦,孩子,是啊,你得有这个麻烦。它驱使我坚果的列表视图和GridView的不能扩展包裹自己的孩子,因为我们都知道,他们除了自己的滚动和他们的孩子的回收更为有利的功能。

Oh boy, yeah, you're gonna have trouble with this one. It drives me nuts that ListViews and GridViews can't be expanded to wrap their children, because we all know that they have more beneficial features in addition to their scrolling and the recycling of their children.

不过,你能砍解决此或创建自己的布局,以满足您的需求不会有太大困难。关闭我的头顶,我能想到的两种可能:

Nonetheless, you can hack around this or create your own layout to suit your needs without too much difficulty. Off the top of my head, I can think of two possibilities:

在我自己的应用程序我都嵌入一个滚动型中的一个ListView。我已经被明确地告诉ListView控件是完全一样高的内容做到了这一点。我这样做是通过改变权 ListView.onMeasure()方法,像这样内部的布局参数:

In my own app I have embedded a ListView within a ScrollView. I have done this by explicitly telling the ListView to be exactly as high as its contents. I do it by changing the layout parameters right inside the ListView.onMeasure() method like so:

public class ExpandableListView extends ListView {

    boolean expanded = false;

    public ExpandableListView(Context context, AttributeSet attrs, int defaultStyle) {
        super(context, attrs, defaultStyle);
    }

    public boolean isExpanded() {
        return expanded;
    }

    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // HACK!  TAKE THAT ANDROID!
        if (isExpanded()) {         
            // Calculate entire height by providing a very large height hint.
            // View.MEASURED_SIZE_MASK represents the largest height possible.
            int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                        MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

这工作,因为当你给ListView控件 AT_MOST 的一种模式,它创建和措施,所有的孩子对你,对里面的 onMeasure 办法(我通过在源$ C ​​$ C浏览发现这一点)。希望GridView控件的行为是相同的,但如果没有,你仍然可以衡量自己的GridView的所有内容。但它会更容易,如果你能欺骗GridView控件到这样做是为了你。

This works because when you give the ListView a mode of AT_MOST, it creates and measures all of its children for you, right inside the onMeasure method (I discovered this by browsing through the source code). Hopefully GridView behaves the same, but if it doesn't, you can still measure all the contents of the GridView yourself. But it would be easier if you could trick the GridView into doing it for you.

现在,你必须记住,该解决方案将完全禁用的观点循环,使GridView中如此有效,以及所有那些ImageViews会在内存中,即使它们是不可见的。同去与我的一个解决方案。

Now, you must keep in mind that this solution would completely disable the view recycling that makes GridView so efficient, and all those ImageViews will be in memory even if they're not visible. Same goes with my next solution.

另一种可能性是沟GridView和创建自己的布局。你可以扩展为 AbsoluteLayout RelativeLayout的。例如,如果你扩展 RelativeLayout的,你可以将每个图像 LEFT_OF 的previous之一,跟踪每幅图像的宽度,直到你运行的空间在该行上,然后通过将新行的第一个图像在过去的最高图像开始的下一行行。为了获得水平居中或等间距列你必须要经过更痛的图像。也许AbsoluteLayout更好。无论哪种方式,一种痛苦。

The other possibility is to ditch the GridView and create your own layout. You could extend either AbsoluteLayout or RelativeLayout. For example, if you extend RelativeLayout, you could place each image LEFT_OF the previous one, keeping track of the width of each image until you run out of room on that row, and then start the next row by placing the first image of the new row BELOW the tallest image of the last row. To get the images horizontally centered or in equally-spaced columns you'll have to go through even more pain. Maybe AbsoluteLayout is better. Either way, kind of a pain.

祝你好运。

这篇关于里面滚动型图像的网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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