我正在制作一个小部件,该小部件的顶部具有网格视图,而其下方是列表视图,当用户从网格中选择时,下面的列表视图将得到更新。 但是我面临这些问题: 首先,在我将其调整两次大小之前,小部件不会显示gridView。 第二,当网格出现时,列表视图将被更新,但是在第二次单击时。 那么无论点击多少次,该值都不会改变。
我尝试在视图上强制执行更新,但是似乎没有任何作用。
这是我的更新小部件功能:
public static int viewIndex = 0;
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
int Height = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
RemoteViews views;
if (Height<300) {
views = new RemoteViews(context.getPackageName(), R.layout.ingredient_widget_provider);
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.appwidget_text,pendingIntent);
}
else{
views = getIngredientsList(context,appWidgetId);
}
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId,R.id.widget_list_view);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
这是设置视图的功能:
private static RemoteViews getIngredientsList(Context context,int appWidgetId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_ings_layout);
Intent intent = new Intent(context,GridWidgetService.class);
views.setRemoteAdapter(R.id.widget_grid_view,intent);
if (viewIndex != 0) {
appWidgetManager.updateAppWidget(appWidgetId, views);
Intent ListIntent = new Intent(context, ListWidgetService.class);
ListIntent.putExtra("extra_id", viewIndex);
views.setRemoteAdapter(R.id.widget_list_view, ListIntent);
}
Intent appIntent = new Intent(context, IngredientWidgetProvider.class);
appIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
appIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent appPendingIntent = PendingIntent.getBroadcast(context, 0,
appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.widget_grid_view, appPendingIntent);
return views;
}
这是onRecieve:
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)){
viewIndex = intent.getIntExtra("extra_id_grid",0);
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
Toast.makeText(context,"position "+viewIndex,Toast.LENGTH_LONG).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
updateAppWidget(context,appWidgetManager,appWidgetId);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId,R.id.widget_list_view);
}
super.onReceive(context, intent);
}
更新:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(context,appWidgetManager,appWidgetIds);
}
public static void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
onOptionsChanged
@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
updateAppWidget(context,appWidgetManager,appWidgetId);
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
}
更新: 我的github链接:https://github.com/basma11elmihy/bakingApp
答案 0 :(得分:0)
最终我能够解决此问题: 原来我不能在远程视图适配器中发出网络请求,这就是为什么它仅在第二次调整大小后才更新,现在 我在意向服务中发出了网络请求,从该服务中我强制进行了更新,如下所示:
public static void InflateList(Context context, int viewIndex) {
Intent intent = new Intent(context,GridIntentService.class);
con = context;
intent.setAction("InflateList");
intent.putExtra("viewIndex",viewIndex);
context.startService(intent);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if(intent != null) {
if (intent.getAction().equals("InflateGrid")) {
getGridData();
}
if (intent.getAction().equals("InflateList")){
int index = intent.getIntExtra("viewIndex",0);
getListData(index);
}
}
}
private void getListData(final int index) {
StringRequest request = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
RecipesModel[] recipesModels = gson.fromJson(response, RecipesModel[].class);
mData = new ArrayList<>(Arrays.asList(recipesModels));
model = mData.get(index);
ings = model.getIngredients();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(con);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(con, IngredientWidgetProvider.class));
// appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_list_view);
IngredientWidgetProvider.update(con,appWidgetManager,appWidgetIds,mData,ings);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error","volley list error");
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}
然后作为上面的代码,我在widgetProvider主类的update函数中的参数中发送了数据, 从那里,我使用意图将数据发送到RemoteViewFactory。 但是事实证明,小部件的意图与活动的意图不同,它不发送Parcalable数据,因此我将Parcalable数据转换为字符串,然后将其作为额外发送。 使用这种方式:
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_ings_layout);
Intent intent = new Intent(context,GridWidgetService.class);
String dumb = new Gson().toJson(mData);
intent.setData(Uri.fromParts("scheme",dumb,null));
views.setRemoteAdapter(R.id.widget_grid_view,intent);
像这样将数据返回到远程适配器中:
public ListRemoteViewFactory(Context context, Intent intent) {
this.context = context;
this.stringData = String.valueOf(intent.getData().getSchemeSpecificPart());
}
因为小部件意图还必须使用setData而不是putExtra像活动意图那样 这将使它不仅一次调用OnGetViewFactory。 其解释如下:
RemoteViewFactory onDataSetChanged() only called once per notifyAppWidgetViewDataChanged()