0

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?

Rupesh
  • 3,415
  • 2
  • 26
  • 30
  • A ViewPager is used for horizontal scrolling of child fragments (with swipe gesture), which is not a good idea as it'll then restrict the horizontal movement of the launcher. – Saket Mar 07 '14 at 10:00
  • that I know @Saket.. but one can disable the swipe in ViewPager by extending it and could achieve the horizontal-scroll/slide from other widget like button.. have a look at ViewPageIndicator library.. – Rupesh Mar 07 '14 at 10:33

2 Answers2

0

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();
Saket
  • 2,945
  • 1
  • 29
  • 31
  • yay!! `ViewPager` is the ans.. but one need to understand the RemoteViews api to implement this.. see my answer.. – Rupesh Mar 12 '14 at 20:17
0

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.

Community
  • 1
  • 1
Rupesh
  • 3,415
  • 2
  • 26
  • 30