Gridview 内 Webview 的 ArrayAdapter
继之前的问题之后,我试图在 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 WebView
s) 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论您如何实现
Adapter
,您都无法可靠地将可滚动小部件放入其他可滚动小部件中,至少在它们以相同方向滚动的情况下是如此。由于WebView
和GridView
都是垂直滚动的,因此尝试以这种方式组合它们时会得到不可靠的结果。接下来,谈谈您的一些陈述和问题:
ArrayAdapter
可以适应您喜欢的任何数据类型的 Java 数组或ArrayList
。第二个构造函数变量是您希望 GridView 中的单元格看起来的样子。在您的情况下,您指定的是
TextView
内置布局资源。因此,您告诉 Android 您希望所有网格单元都是TextView
小部件。 Android 将对数组中的对象调用toString()
,将结果倒入TextView
中,然后TextViews
将进入网格单元格。如果您希望
ArrayAdapter
返回TextView
以外的内容,则需要重写getView()
并自行处理更多内容,可能使用您自己创建的布局文件。正如我在回答开头所指出的,您想要的根本无法可靠地工作。
暂时假设在
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 bothWebView
andGridView
scroll vertically, you will get unreliable results trying to combine them this way.With that, on to some of your statements and questions:
An
ArrayAdapter
can adapt a Java array or anArrayList
of whatever data type you like.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 aTextView
. Hence, you are telling Android you want all your grid cells to beTextView
widgets. Android will calltoString()
on the objects in your array, pour that result into theTextView
, and theTextViews
will go in your grid cells.If you want your
ArrayAdapter
to be returning things other than aTextView
, you will need to overridegetView()
and handle more of that yourself, possibly using a layout file of your own creation.As noted at the outset of my answer, what you want simply will not work reliably.
Pretending for the moment that having
WebViews
in aGridView
would work, the simplest solution would be for you to overridegetView()
in your own subclass ofArrayAdapter
, as I mentioned previously. Here is a free excerpt from one of my books that goes over this process.