Android 无法使用列表视图加载主屏幕小部件
Posted
技术标签:
【中文标题】Android 无法使用列表视图加载主屏幕小部件【英文标题】:Android failed to load homescreen widget with List view 【发布时间】:2018-08-05 01:45:21 【问题描述】:我目前正在为一个项目编写一个简单的食谱应用程序,我遇到了一个需要帮助的问题。提前致谢。
问题: android 小部件将在没有激活列表视图的情况下完美加载,但如果它被激活,它将无法加载整个小部件。
以下是代码: 远程工厂视图
public class Widget_Service extends RemoteViewsService
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent)
return new Remote_Ingredients_Adapter(this.getApplicationContext(), intent);
public class Remote_Ingredients_Adapter implements RemoteViewsService.RemoteViewsFactory
private Context context;
private ArrayList<Ingredients> ingredients_list;
public Remote_Ingredients_Adapter(Context context, Intent intent)
this.context = context;
this.ingredients_list = intent.getParcelableArrayListExtra(EasyBakeWidget.Intent_Ingredients);
@Override
public void onCreate()
@Override
public void onDataSetChanged()
@Override
public void onDestroy()
@Override
public int getCount() return ingredients_list.size();
@Override
public RemoteViews getViewAt(int i)
RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.ingredients_adapter_layout);
Ingredients ingredient = ingredients_list.get(i);
row.setTextViewText(R.id.tv_ingredient_name,ingredient.getIngredient());
row.setTextViewText(R.id.tv_ingredient_quantity,ingredient.getQuantityWithMeasure());
return row;
@Override
public RemoteViews getLoadingView() return null;
@Override
public int getViewTypeCount() return 1;
@Override
public long getItemId(int i) return i;
@Override
public boolean hasStableIds() return true;
小部件提供程序代码
Intent rlv_intent = new Intent(context,Widget_Service.class);
rlv_intent.putParcelableArrayListExtra(Intent_Ingredients, recipe.getIngredients());
widget.setRemoteAdapter(R.id.lv_widget_ingredients,rlv_intent);
完整的小部件布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="@drawable/curved_border_with_fill"
android:orientation="vertical"
android:padding="@dimen/widget_margin">
<RelativeLayout
android:layout_
android:layout_
android:padding="@dimen/Padding_Large"
android:layout_marginTop="@dimen/Padding_Medium">
<ImageButton
android:layout_
android:layout_
android:id="@+id/widget_back_button"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_keyboard_arrow_left"
android:layout_alignBaseline="@id/widget_forward_button"
android:background="@android:color/transparent"/>
<TextView
android:layout_
android:layout_
android:id="@+id/widget_recipe_name"
android:textSize="25sp"
tools:text="recipe_name"
android:layout_alignParentTop="true"
android:layout_toEndOf="@id/widget_back_button"
android:layout_toStartOf="@id/widget_forward_button"
android:layout_alignBaseline="@id/widget_back_button"
android:gravity="center"/>
<ImageButton
android:layout_
android:layout_
android:id="@+id/widget_forward_button"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_keyboard_arrow_right"
android:background="@android:color/transparent"/>
</RelativeLayout>
<include layout="@layout/divider_line_solid"/>
<ListView
android:layout_
android:layout_
android:padding="@dimen/Padding_Large"
android:id="@+id/lv_widget_ingredients"
android:layout_gravity="center"/>
</LinearLayout>
列表视图布局 (R.id.lv_widget_ingredients) 注意此布局文件用于 2 个地方。其中一个活动中的小部件列表视图和回收器视图,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="horizontal">
<TextView
android:id="@+id/tv_ingredient_name"
style="@style/Body_Custom"
android:layout_weight="0.6"
android:layout_
android:layout_
android:layout_margin="@dimen/Padding_Medium"
android:textAlignment="textStart"
android:maxLines="3"
tools:text="Ingredient Name" />
<TextView
android:id="@+id/tv_ingredient_quantity"
style="@style/Body_Custom"
android:layout_weight="0.4"
android:layout_
android:layout_
android:layout_margin="@dimen/Padding_Medium"
android:textAlignment="textEnd"
tools:text="Ingredient quantity" />
</LinearLayout>
Android 清单代码
<receiver android:name=".components.EasyBakeWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/easy_bake_widget_info" />
</receiver>
<service
android:name=".components.Widget_Service"
android:permission="android.permission.BIND_REMOTEVIEWS" />
【问题讨论】:
【参考方案1】:将我的 logcat 更改为无过滤器模式让我可以看到来自 Home 启动器的错误。错误是
Class not found when unmarshalling: com.andre_fernando.easybakerecipes.data_objects.Ingredients
java.lang.ClassNotFoundException: com.andre_fernando.easybakerecipes.data_objects.Ingredients
所以我将自定义成分模型的 ArrayList 转换为 2 个字符串 ArrayList。
Intent lv_intent = new Intent(context,Widget_Service.class);
ArrayList<Ingredients> ingredients_list = recipe.getIngredients();
ArrayList<String> ingredient_name = new ArrayList<>();
ArrayList<String> ingredient_quantity = new ArrayList<>();
for (Ingredients i: ingredients_list)
ingredient_name.add(i.getIngredient());
ingredient_quantity.add(i.getQuantityWithMeasure());
lv_intent.putStringArrayListExtra("name",ingredient_name);
lv_intent.putStringArrayListExtra("quantity",ingredient_quantity);
widget.setRemoteAdapter(R.id.widget_ingredients_list,lv_intent);
这修复了小部件。
同样使用内容提供者来获取数据也修复了小部件。
【讨论】:
以上是关于Android 无法使用列表视图加载主屏幕小部件的主要内容,如果未能解决你的问题,请参考以下文章
如何将图像从 url 加载到小部件的 android 远程视图中
如何在android中获取主屏幕小部件列表项的点击事件[重复]
是否可以使用 Ionic Framework 或 React Native 为 Android 创建主屏幕小部件?