Gridview 内 Webview 的 ArrayAdapter

发布于 2024-12-29 16:25:59 字数 1191 浏览 2 评论 0原文

继之前的问题之后,我试图在 GridView 中获取多个(小部件或在本例中为 WebView)。

所以我不太确定如何去做,是制作我自己的适配器(当时看起来很可怕)还是创建一个 ArrayAdapter

我不确定 ArrayAdapter 是否可以像这样接受它或者它是否只支持原始类型。

GridView contentGrid;
LinearLayout contentLayout;

WebWidget[] webWidgets;

WebWidget web1;
WebWidget web2;
WebWidget web3;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    web1 = new WebWidget(this, "http://www.google.ie");
    web2 = new WebWidget(this, "http://www.facebook.com");
    web3 = new WebWidget(this, "http://www.youtube.ie");

    webWidgets = new WebWidget[]{web1,web2,web3};

    contentGrid = (GridView) findViewById(R.id.contentGrid);

           //Heres my attempt at an adapter for webview
    ArrayAdapter<WebView> adapter = new ArrayAdapter<WebView>(this,
            android.R.layout.simple_list_item_1, webWidgets);

    contentGrid.setAdapter(adapter);

不幸的是,出现的是 WebView 的字符串版本。

所以问题是我可以这样做还是我自己制作 CustomAdapter 更好?

注意:有人可以指出一些关于第二个构造函数变量是什么的信息吗? IE android.R.layout.simple_list_item_1

Following on from an earlier question I'm trying to get multiple (widgets or in this case WebViews) inside a GridView.

So I wasn't really sure how to go about it whether to make my own adapter (seemed scary at the time) or create an ArrayAdapter<WebView>.

I wasn't really sure if ArrayAdapter could accept it like this or if it only supported primitive types.

GridView contentGrid;
LinearLayout contentLayout;

WebWidget[] webWidgets;

WebWidget web1;
WebWidget web2;
WebWidget web3;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    web1 = new WebWidget(this, "http://www.google.ie");
    web2 = new WebWidget(this, "http://www.facebook.com");
    web3 = new WebWidget(this, "http://www.youtube.ie");

    webWidgets = new WebWidget[]{web1,web2,web3};

    contentGrid = (GridView) findViewById(R.id.contentGrid);

           //Heres my attempt at an adapter for webview
    ArrayAdapter<WebView> adapter = new ArrayAdapter<WebView>(this,
            android.R.layout.simple_list_item_1, webWidgets);

    contentGrid.setAdapter(adapter);

Unfortunately what comes out is the string version of the WebView.

So question is can I do it this way or am I better of making my own CustomAdapter?

NOTE: Could someone maybe point some information as to what the second constructor variable is? I.E android.R.layout.simple_list_item_1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生一梦 2025-01-05 16:25:59

无论您如何实现Adapter,您都无法可靠地将可滚动小部件放入其他可滚动小部件中,至少在它们以相同方向滚动的情况下是如此。由于 WebViewGridView 都是垂直滚动的,因此尝试以这种方式组合它们时会得到不可靠的结果。

接下来,谈谈您的一些陈述和问题:

我不太确定 ArrayAdapter 是否可以这样接受它,或者它是否只支持原始类型。

ArrayAdapter 可以适应您喜欢的任何数据类型的 Java 数组或 ArrayList

不幸的是,出现的是 webview 的字符串版本...注意:有人可以指出一些关于第二个构造函数变量是什么的信息吗? IE android.R.layout.simple_list_item_1

第二个构造函数变量是您希望 GridView 中的单元格看起来的样子。在您的情况下,您指定的是 TextView 内置布局资源。因此,您告诉 Android 您希望所有网格单元都是 TextView 小部件。 Android 将对数组中的对象调用 toString(),将结果倒入 TextView 中,然后 TextViews 将进入网格单元格。

如果您希望 ArrayAdapter 返回 TextView 以外的内容,则需要重写 getView() 并自行处理更多内容,可能使用您自己创建的布局文件。

所以问题是我可以这样做还是我自己制作 CustomAdapter 更好?

正如我在回答开头所指出的,您想要的根本无法可靠地工作。

暂时假设在 GridView 中使用 WebViews 可以工作,最简单的解决方案是在您自己的子类中重写 getView()正如我之前提到的,ArrayAdapter 的。这是我的一本书中的免费摘录,介绍了这个过程。

No matter how you implement your Adapter, you cannot reliably put scrollable widgets in other scrollable widgets, at least where they scroll in the same direction. Since both WebView and GridView scroll vertically, you will get unreliable results trying to combine them this way.

With that, on to some of your statements and questions:

I wasn't really sure if ArrayAdapter could accept it like this or if it only supported primitive types.

An ArrayAdapter can adapt a Java array or an ArrayList of whatever data type you like.

Unfortunately what comes out is the string version of the webview...NOTE: Could someone maybe point some information as to what the second constructor variable is? I.E android.R.layout.simple_list_item_1

The second constructor variable is what you want the cells in your GridView to look like. In your case, you are specifying a built-in layout resource that is a TextView. Hence, you are telling Android you want all your grid cells to be TextView widgets. Android will call toString() on the objects in your array, pour that result into the TextView, and the TextViews will go in your grid cells.

If you want your ArrayAdapter to be returning things other than a TextView, you will need to override getView() and handle more of that yourself, possibly using a layout file of your own creation.

So question is can I do it this way or am i better of making my own CustomAdapter?

As noted at the outset of my answer, what you want simply will not work reliably.

Pretending for the moment that having WebViews in a GridView would work, the simplest solution would be for you to override getView() in your own subclass of ArrayAdapter, as I mentioned previously. Here is a free excerpt from one of my books that goes over this process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文