ViewFlipper in app widgets

空扰寡人 提交于 2019-12-18 13:32:00

问题


I'm playing around building a new widget and was looking at the Android app widget documentation, in particular the section on which widget classes were supported. I noticed that ViewFlipper is supported, but I'm struggling to find any examples on how to use one in a home screen widget. In particular, I'm wondering if its possible to manually trigger swapping views. In an activity this looks relatively straightforward, one example being hooking up the onclick listener of a button to call the showNext() of the flipper.

The RemoteViews object has showNext and showPrevious methods but I'm not sure I understand how to hook them up to an event fired from the user interacting with the widget. Can anyone provide examples of when these methods might be called?

It looks like buttons in widgets can only be wired up to intents rather than code to exercise the flipper. If this restriction is true, then is the only use of a view flipper in an app widget for auto flipping of views?


回答1:


Say you have 2 buttons: LEFT and RIGHT. First you would attach pending intent to each (here it's triggered from Service#onStart:

@Override
public void onStart(Intent intent, int startId) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
    int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
    // add listeners for every widget registered
    for (int widgetId : allWidgetIds) {
        addClickListeners(appWidgetManager, widgetId, root);
    }
    stopSelf();
}

protected void addClickListeners(AppWidgetManager appWidgetManager, int widgetId, RemoteViews root) {
    root.setOnClickPendingIntent(R.id.left, getNavigationIntent(widgetId, R.id.left));
    root.setOnClickPendingIntent(R.id.right, getNavigationIntent(widgetId, R.id.right));
}

protected PendingIntent getNavigationIntent(int widgetId, final int id) {
    Intent clickIntent = new Intent(this, WidgetProvider.class);
    clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
    clickIntent.putExtra(TRIGGER, id);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    return pendingIntent;
}

Then, in AppWidgetProvider do

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Bundle extras = intent.getExtras();
    Integer id = (Integer) (extras == null ? null : extras.get(TRIGGER));
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action) && id != null) {
        int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
        onNavigate(context, widgetId, id); 
    } else {
        super.onReceive(context, intent);
    }
}


protected void onNavigate(Context context, Integer widgetId, Integer id) {  
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    RemoteViews root = new RemoteViews(context.getPackageName(), R.layout.app_widget);
    if (id == R.id.left) {
        root.showPrevious(R.id.scroll);
    } else {
        root.showNext(R.id.scroll);            
    }
    appWidgetManager.updateAppWidget(widgetId, root);
}

This should do it. Now the problem is - this will only work in API 11+ and I just found the hard way that root.setInt(R.id.scroll, "setDisplayedChild", pos) will not work in API 7.



来源:https://stackoverflow.com/questions/8282452/viewflipper-in-app-widgets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!