ViewFlipper in application widgets

I play around creating a new widget and look at the documentation for widgets for Android applications , in particular the section that supports widget classes. I noticed that ViewFlipper is supported , but I'm struggling to find examples of how to use them in home screen widgets. In particular, I am wondering if it is possible to manually run swapping views. In activity, this looks relatively simple, one example is to connect the onclick listener of a button to call the showNext () flipper.

The RemoteViews object has the showNext and showPrevious methods, but I'm not sure I understand how to connect them to an event triggered by the user interacting with the widget. Can someone give examples of when these methods can be called?

It seems that the buttons in the widgets can only be connected to intentions, and not to the code for using the flipper. If this restriction is true, is this the only use of the view flipper in application widgets to automatically scroll through views?

+5
source share
1 answer

, : LEFT RIGHT. ( 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;
}

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

. , API 11+, , root.setInt(R.id.scroll, "setDisplayedChild", pos) API 7.

+8

All Articles