gpt4 book ai didi

android - 如何修复第二次点击后更新的小部件 View

转载 作者:行者123 更新时间:2023-11-29 02:22:57 26 4
gpt4 key购买 nike

我正在制作一个小部件,顶部有一个 GridView ,下面有一个 ListView ,当用户从网格中选择时,下面的 ListView 会更新。但我面临这些问题:首先,在我重新调整大小两次之前,小部件不会显示 gridView。其次,当网格出现时, ListView 得到更新,但在第二次单击时。那么无论我点击它多少次,该值都不会改变。

我试过强制更新 View ,但似乎没有任何效果。

这是我的更新小部件功能:

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);

}

这里是设置 View 的函数:

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

最佳答案

最终我能够解决这个问题: 结果我无法在我正在做的远程 View 适配器中发出网络请求,这就是为什么它只在第二次调整大小后更新,现在我在 Intent 服务中发出网络请求,我从该服务强制更新,如下所示:

 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函数的参数中发送了数据,从那里我使用 Intent 将数据发送到 RemoteViewFactory。但事实证明小部件 Intent 与 Activity Intent 不同,它不发送 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());

}

因为widget intent也得用setData而不是putExtra 像activity Intent这将使它每次都调用 OnGetViewFactory 而不是一次。在这里解释:

RemoteViewFactory onDataSetChanged() only called once per notifyAppWidgetViewDataChanged()

关于android - 如何修复第二次点击后更新的小部件 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54102301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com