【问题标题】:How to refresh widget listview when button refresh is clicked?单击按钮刷新时如何刷新小部件列表视图?
【发布时间】:2017-01-16 06:05:06
【问题描述】:

我正在创建一个带有ListViewWidget。在ListView 中显示来自Web Service 的结果是可以的。我创建了一个类,它是我的WidgetService,它扩展到RemoteViewsService,在RemoteViewsFactory 上,我调用我的ListViewProvider,它具有Web Service。我添加了一个刷新按钮,单击该按钮将再次调用WidgetService 以创建列表视图。

这是我在WidgetProvider 中的代码

public class WidgetProvider extends AppWidgetProvider {

   @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                     int[] appWidgetIds) {
 final int N = appWidgetIds.length;
  for (int i = 0; i < N; ++i) {
        RemoteViews remoteViews = updateWidgetListView(context,
                appWidgetIds[i]);
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listViewWidget);
        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

private RemoteViews updateWidgetListView(Context context, int appWidgetId) {

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget_layout);

//ListView
    Intent svcIntent = new Intent(context, WidgetService.class);
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget, svcIntent);
    remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);

    //REFRESH
    PendingIntent updatePendingIntent = PendingIntent.getService(context, 0, svcIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.ivRefreshWidget, updatePendingIntent);

 return remoteViews;
}

正如您在刷新部分中看到的,我尝试使用PendingIntent 调用WidgetService,但在测试时它不会刷新列表。 提前感谢您的帮助。

【问题讨论】:

  • 我之前已经回答过了 - stackoverflow.com/a/12907825/726863 另外,这里也是 - stackoverflow.com/a/36005649/726863
  • @LalitPoptani 基于链接我应该使用Service 而不是使用RemoteViewsService 作为我的WidgetService
  • 还有@LalitPoptani 我在我的WidgetProvider 上使用notifyAppWidgetViewDataChanged 我应该把它放在别的地方吗?我想在单击按钮时触发刷新。

标签: android listview android-widget


【解决方案1】:

在我的onUpdate中,我设置了一个要在我的onReceive中调用的动作,并将appWidgetIds也放入onReceive中使用

Intent refreshIntent = new Intent(context, WidgetProvider.class);
        refreshIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

然后在onReceive我打电话给onUpdate

if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            if (appWidgetIds != null && appWidgetIds.length > 0) {
                Toast.makeText(context, "REFRESH", Toast.LENGTH_SHORT).show();
                this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
            }
        }
    }

【讨论】:

  • 那么第一个代码将用于按钮的 onclickpendingintent 吗?好吧,那没有用。我在小部件中有一个刷新按钮,我想用它来刷新小部件本身并调用onUpdate。有什么想法吗?
  • @Si8 首先你需要在onUpdate 中设置按钮然后设置一个动作,对我来说是AppWidgetManager.ACTION_APPWIDGET_UPDATE 然后在onReceive 中获取动作你将调用this.onUpdate()
  • @Si8 我回答了你的问题,看看谢谢
【解决方案2】:
arrayList = new ArrayList<AllPostsProvider>();
arrayList.add(all_contents);
AllPostReCyclerViewAdapter = new AllPostReCyclerViewAdapter(arrayList,getApplicationContext());

当点击刷新按钮时

 arrayList.clear();
 arrayList = new ArrayList<AllPostsProvider>();
 arrayList.add(all_contents);
AllPostReCyclerViewAdapter = new AllPostReCyclerViewAdapter(arrayList,getApplicationContext());

【讨论】:

  • 当你调用刷新按钮 onclick 函数时......它会在里面
  • 你能举个例子吗,因为我正在使用remoteViews.setOnClickPendingIntent(R.id.ivRefreshWidget, pendingServiceIntent);
猜你喜欢
  • 2022-06-11
  • 1970-01-01
  • 2021-11-04
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多