如何将 GridView 放入 ScrollView [英] How to put GridView inside ScrollView

查看:23
本文介绍了如何将 GridView 放入 ScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须设计布局,使整个布局应该滚动,并且在布局内部我必须以 Grid 形式显示相关内容,所以我决定使用 GridView.但问题是我无法在 ScrollView 内使用 GridView 我已经阅读了文档(文档还说我们不应该在 ScrollView 内使用 GridView) 也.但我必须这样做,所以请给我一些关于这个的想法.谢谢

I have to design layout such that whole layout should scroll and inside layout I have to display related content in Grid form so I decided to use GridView.But problem is that I’m unable to use GridView inside ScrollView I have read documentation(Documentation also saying that we should not use GridView inside ScrollView) also.But I have to do this,so can any please give me some idea about this.Thanks

推荐答案

除了固有的滚动之外,GridView 肯定还有一些好处.例如,一个一致的、动态的单元格布局,它会根据你传入的数据进行扩展和收缩.因此,当人们说想要这样的功能不好时,我认为这是错误的,因为您可能希望在滚动视图中包含动态图像(视图)网格,但希望整个滚动视图包含除网格之外的其他内容细胞.

There are definitely benefits to a GridView beside the inherent scrolling. For example, a consistent, dynamic layout of cells that will expand and contract based on the data you pass into it. So, when people say it's not good to desire such a functionality, I think that's wrong because you could want the dynamic grid of images (views) inside of a scrolling view, but want that entire scrolling view to contain other things than just the grid cells.

现在,您可以这样做.在此处查看答案.它是一个可扩展的高度 GridView,您需要在项目中导入/创建它.这基本上意味着随着更多项目添加到 GridView,它只会扩展其高度,而不是保持其高度设置并使用滚动.这正是您想要的.

Now, here is how you can do this. Check the answer here. It is an expandable height GridView, which you will want to import / create in your project. What that basically means is that as more items are added to the GridView, it will just expand its height, as opposed to keeping its height set and using scrolling. This is exactly what you want.

在您的项目中拥有 ExpandableHeightGridView 后,转到您希望 GridView 所在的 XML 布局.然后你可以做这样的事情(释义):

Once you have the ExpandableHeightGridView in your project, go to your XML layout where you want the GridView to be. You can then do something like this (paraphrasing):

<ScrollView ...>
    <RelativeLayout ...>
        <com.example.ExpandableHeightGridView ... />
        <other view items />
    </RelativeLayout>
</ScrollView>

然后,在您设置 GridView 适配器的活动中,您要确保将其设置为展开.所以:

Then, in your activity where you set the GridView's adapter, you want to make sure you set it to expand. So:

ExpandableHeightGridView gridView = (ExpandableHeightGridView) findViewById(R.id.myId);
gridView.setAdapter(yourAdapter);
gridView.setExpanded(true);

您想要这个可扩展的 GridView 的原因是,标准 GridView 不扩展的事实是导致它滚动的原因.它坚持一定的高度,然后随着更多的项目填充它超过它的视图边界,它变得可滚动.现在,有了这个,您的 GridView 将始终扩展其高度以适应其中的内容,因此永远不允许它进入其滚动模式.这使您可以在 ScrollView 内使用它,并在 ScrollView 内使用其上方或下方的其他视图元素,并让它们全部滚动.

The reason you want this expandable GridView is because, the fact that a standard GridView doesn't expand is what causes it to scroll. It sticks to a certain height, and then as more items fill it past its view bounds, it becomes scrollable. Now, with this, your GridView will always expand its height to fit the content within it, thus never allowing it to enter its scrolling mode. This enables you to use it inside of the ScrollView and use other view elements above or below it within the ScrollView, and have them all scroll.

这应该会给你你正在寻找的结果.如果您有任何问题,请告诉我.

This should give you the result you're looking for. Let me know if you have any questions.

这篇关于如何将 GridView 放入 ScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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