Android:向 ListView App Widget 添加多个视图
我正在尝试使用 3.1 SDK 创建一个主屏幕小部件。我遵循了 StackWidget 教程。并将 StackView
更改为 ListView
。 我想向列表中的每一行添加图像和文本,这些是在运行时加载的。
我在我的 WidgetProvider.java
类中使用此代码,
public RemoteViews getViewAt(int position) {
Bitmap bitmap = ((BitmapDrawable)mWidgetItems.get(position).getImage());
RemoteViews image = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
image.setImageViewBitmap(R.id.widget_image, bitmap );
RemoteViews text = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
text.setTextViewText(R.id.widget_text, mWidgetItems.get(position).getText());
RemoteViews layout = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
Bundle extras = new Bundle();
extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
layout.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
return layout;
}
当我使用 StackView
时,所有这些都可以正常工作,但更改为 ListView
会搞乱一切。结果是
只能返回一个视图。所以,如果我添加下面的代码,就会出现这个问题,如果我不添加,那么列表的内容为空。
layout.addView(R.id.widget_item, image);
layout.addView(R.id.widget_item, text);
我还能如何向小部件添加多个视图? 为什么这对 StackView 有效,但对 ListView 无效? 我该如何纠正这个问题?
非常感谢任何帮助。
编辑
widget_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:id="@+id/widget_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="@+id/widget_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_weight="1"
android:textColor="#000000"/>
</LinearLayout>
I am trying to create a homescreen widget using the 3.1 SDK. I followed the StackWidget tutorial. and changed the StackView
to ListView
.
I want to add an image and text to each row in my list, and these are loaded at runtime.
I use this code in my WidgetProvider.java
class
public RemoteViews getViewAt(int position) {
Bitmap bitmap = ((BitmapDrawable)mWidgetItems.get(position).getImage());
RemoteViews image = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
image.setImageViewBitmap(R.id.widget_image, bitmap );
RemoteViews text = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
text.setTextViewText(R.id.widget_text, mWidgetItems.get(position).getText());
RemoteViews layout = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
Bundle extras = new Bundle();
extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
layout.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
return layout;
}
All of this works correctly when I use StackView
, but changing to ListView
messes up everything. The result is this
Only one view can be returned. So, if I add the following code, this problem occurs, if I don't add it, then the content of the list is empty.
layout.addView(R.id.widget_item, image);
layout.addView(R.id.widget_item, text);
How else do I add multple views to a widget?
Why does this work for StackView but not ListView?
and how do I rectify this?
Any help is greatly appreciated.
EDIT
widget_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:id="@+id/widget_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="@+id/widget_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_weight="1"
android:textColor="#000000"/>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过从一开始就膨胀正确的布局来纠正这个问题。
R.layout.widget_image
应该已经拥有R.layout.widget_image
和R.layout.widget_item
中的所有内容。这将是:
而且,作为额外的好处,它应该适用于任何受支持的基于 AdapterView 的应用程序小部件。
You rectify this by inflating the right layout from the outset.
R.layout.widget_image
should already have whatever stuff you have inR.layout.widget_image
andR.layout.widget_item
.This will be:
And, as an extra bonus, it should work for any supported
AdapterView
-based app widget.