ViewFlipper在app部件

ViewFlipper在app部件

本文介绍了ViewFlipper在app部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩弄建设一个新的小部件,并一直在寻找在 Android应用窗口小部件文档,特别是其中部件类部分的支持。我注意到, ViewFlipper 是支持的,但我在努力寻找任何例子就如何在主屏幕小部件使用一个。我特别想知道,如果它可以手动触发交换意见。在活动中,这看起来相对简单,其中一个例子就是挂钩一个按钮来调用的鳍状肢的showNext()的onclick监听器。

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.

借助 RemoteViews 对象有showNext并显示previous方法但我不知道我知道如何挂钩起来,从与小部件交互的用户触发的事件。任何人都可以提供,当这些方法可以被称为的例子?

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?

它看起来像小部件的按钮只能连接最多的意图,而不是code行使的鳍状肢。如果这个限制是真的,那是在一个应用程序窗口小部件的看法自动翻转?

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?

推荐答案

假设你有2个按钮:左,右。首先,你会附加未决意图每个(在这里它是从触发服务#ONSTART

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

然后,在 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.

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.

这篇关于ViewFlipper在app部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 04:01