如何自动前进 ListView 应用程序小部件(如 Honeycomb 上的 StackView 应用程序小部件)?

发布于 2024-11-15 07:11:20 字数 788 浏览 2 评论 0原文

我正在寻找一种定期动画/自动推进 ListView 应用程序小部件的方法,就像使用 StackView 应用程序小部件完成的那样。

例如,Android Market 和 YouTube 小部件每 20 秒就会自动前进到集合中的下一个项目(在 Honeycomb 上)。

我在 StackView Widget 示例 中看到了他们使用的StackView 小部件的 autoAdvanceViewId 设置:

<appwidget-provider
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="150dip"
  android:minHeight="150dip"
  android:updatePeriodMillis="3600000"
  android:previewImage="@drawable/preview"
  android:initialLayout="@layout/widget_layout"
  android:autoAdvanceViewId="@id/stack_view">
</appwidget-provider>


对于 ListView 应用程序小部件,可以以某种方式完成或模拟吗?

非常感谢,
洛朗

I'm looking for a way to animate/auto-advance periodically a ListView app widget as it is done with StackView app widgets.

For example, the Android Market and YouTube widgets do animate every 20s by auto advancing to the next item in the collection (on Honeycomb).

I have seen in the StackView Widget example that they use a autoAdvanceViewId setting for the StackView widget :

<appwidget-provider
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="150dip"
  android:minHeight="150dip"
  android:updatePeriodMillis="3600000"
  android:previewImage="@drawable/preview"
  android:initialLayout="@layout/widget_layout"
  android:autoAdvanceViewId="@id/stack_view">
</appwidget-provider>

Can this be done or emulated in some way for a ListView app widget?

Many thanks,
Laurent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初熏 2024-11-22 07:11:20

我找到了一种解决方案,但它可能并不理想!

我在AppWidgetProvider的onEnable()中设置了一个重复警报,并在onDisabled()中取消了它。该警报会触发 BroadcastReceiver,更新小部件视图并在 RemoteView 上使用 setRelativeScrollPosition()

下面是几行代码:

  • 在扩展 AppWidgetProvider 的类中

    public void onEnabled(Context context) {
        [...]
      // 创建每 n 秒移动一次小部件列表内容的警报
      AlarmManager AlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    
      长firstTime = SystemClock.elapsedRealtime();
      第一次 += 10*1000;
    
      Intent i = new Intent(context, WidgetAutoScroller.class);
      PendingIntent widgetAutoScroll = PendingIntent.getBroadcast(context, 0, i, 0);
      AlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,firstTime, 
            10*1000, widget自动滚动);
      super.onEnabled(上下文);
    }
    
    @覆盖
    公共无效onDisabled(上下文上下文){
      // 停用移动小部件内容的警报 
      Intent i = new Intent(context, WidgetAutoScroller.class);
      PendingIntent widgetAutoScroll = PendingIntent.getBroadcast(context, 0, i, 0);
      AlarmManager AlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      AlarmManager.cancel(widgetAutoScroll);
      super.onDisabled(上下文);
    }
    
  • WidgetAutoScroller.java


public class WidgetAutoScroller extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, CascadeWidgetProvider.class));

        for( int i=0 ; i<appWidgetIds.length ; i++) {
            int appWidgetId = appWidgetIds[i];
            RemoteViews rv = CascadeWidgetProvider.getWidgetView(context, appWidgetId);
            rv.setRelativeScrollPosition(R.id.widget_collection, 1);
            appWidgetManager.updateAppWidget(appWidgetId, rv);
        }
    }
}

如果您对我的问题有更好的解决方案,请回答。

编辑:另一个问题是:当我到达清单末尾时该怎么办?
我想转到列表的第一项,但我不知道如何从 RemoteView 或 appWidgetId 获取列表的当前位置...

I found one solution, but it might not be ideal!

I set a repeating alarm in the onEnable() of the AppWidgetProvider and cancel it in the onDisabled(). That alarm triggers a BroadcastReceiver that updates the widget views and use setRelativeScrollPosition() on the RemoteView.

Here's a few lines of code:

  • In the class extending AppWidgetProvider

    public void onEnabled(Context context) {
        [...]
      // create alarm that will move the widget list content every n seconds
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    
      long firstTime = SystemClock.elapsedRealtime();
      firstTime += 10*1000;
    
      Intent i = new Intent(context, WidgetAutoScroller.class);
      PendingIntent widgetAutoScroll = PendingIntent.getBroadcast(context, 0, i, 0);
      alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, 
            10*1000, widgetAutoScroll);
      super.onEnabled(context);
    }
    
    @Override
    public void onDisabled(Context context) {
      // deactivate the alarm that moves the widget content 
      Intent i = new Intent(context, WidgetAutoScroller.class);
      PendingIntent widgetAutoScroll = PendingIntent.getBroadcast(context, 0, i, 0);
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      alarmManager.cancel(widgetAutoScroll);
      super.onDisabled(context);
    }
    
  • WidgetAutoScroller.java


public class WidgetAutoScroller extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, CascadeWidgetProvider.class));

        for( int i=0 ; i<appWidgetIds.length ; i++) {
            int appWidgetId = appWidgetIds[i];
            RemoteViews rv = CascadeWidgetProvider.getWidgetView(context, appWidgetId);
            rv.setRelativeScrollPosition(R.id.widget_collection, 1);
            appWidgetManager.updateAppWidget(appWidgetId, rv);
        }
    }
}

If you have a better solution to my problem, please answer.

EDIT: Another problem is: what to do when I reach the end of my list?
I would want to go to the first item of the list, but I don't know how to get the current position of the list from my RemoteView or appWidgetId ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文