Frage

I am writing an app widget for very first time, and came to know that there are too many restrictions on it.

I know I can't use ViewPager and any custom view inside an app widget, but I need to use ViewPager's left-right sliding effect. Please have a look at this..

enter image description here

Requirement is, whenever user taps on left arrow (top left corner) next list will slide in from right, there are three lists to be displayed one by one, and this will go circularly, ie. -L1-L2-L3-L1-

is there any way to implement this?

War es hilfreich?

Lösung

If gestures are not what you seek, then you should simply use a ViewFlipper. It's similar to ViewPager except that it does not support gestures and requires pages to be changed pragmatically.

So when your user presses the left arrow, you can simply call viewPager.showPrev() to go back to the previous page.

You can also add page enter / exit animations in this way:

viewFlipper.setInAnimation(context, R.anim.page_in_animation);
viewFlipper.setOutAnimation(context, R.anim.page_out_animation);
viewFlipper.showPrev();

Andere Tipps

used ViewFlipper to implement mentioned behavior.

following is the code of extended AppWidgetProvider

public static final String TRIGGER = "trigger";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
    int[] allAppWidgetIds) {

    for (int appWidgetId : allAppWidgetIds) {
         RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
             R.layout.layout_widget);

         Intent clickIntent = new Intent(context, CueWidgetProvider.class);
         clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
         clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
         clickIntent.putExtra(CueWidgetProvider.TRIGGER, 
             R.id.app_wid_fltr_next_btn);

         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 
              0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
         remoteViews.setOnClickPendingIntent(R.id.app_wid_fltr_next_btn, 
            pendingIntent);

         appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
}

@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.layout_widget);

    if (id == R.id.app_wid_fltr_next_btn) {
        root.showNext(R.id.app_wid_fltr_flipper);
    }

    appWidgetManager.updateAppWidget(widgetId, root);
}

referred answer to this question.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top