当ViewFlipper为Widget时,更新ViewFlipper的子视图

时间:2013-11-08 03:43:52

标签: android widget viewflipper

当我使用ViewFlipper作为Widget时,我在ViewFlipper中更新子视图时遇到问题。

以下是小工具的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Widget Title"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/refresh_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Refresh" />

<ViewFlipper
    android:id="@+id/viewflipper"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/refresh_button"
    android:autoStart="true"
    android:inAnimation="@anim/in_from_bottom"
    android:outAnimation="@anim/out_from_top"
    android:flipInterval="10000" >

    <TextView
        android:name="@+id/test1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF00FF"
        android:text="test1" />

    <TextView
        android:name="@+id/test2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00FF00"
        android:text="test2" />
</ViewFlipper>

这是AppWidgetProvider:

package com.example.filperwidget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

public class FilperWidgetProvider extends AppWidgetProvider {
private static final String TAG = FilperWidgetProvider.class.getSimpleName();
private static final String REFRESH_ACTION = "com.example.fliperwidget.REFRESH";
private RemoteViews mRemoteViews;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    for (int i = 0; i < appWidgetIds.length; i++) {
        int widgetId = appWidgetIds[i];
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);
        remoteViews.setTextViewText(R.id.test1, "flipper");
        initRefreshButton(context, remoteViews, widgetId);
        mRemoteViews = remoteViews;
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

private void initRefreshButton(Context context, RemoteViews remoteViews, int appWidgetId) {
    // Bind the click intent for the refresh button on the widget
    final Intent refreshIntent = new Intent(context,
            FilperWidgetProvider.class);
    refreshIntent.setAction(FilperWidgetProvider.REFRESH_ACTION);
    refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(
            context, 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.refresh_button,
            refreshPendingIntent);
}

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equals(REFRESH_ACTION)) {
        Toast.makeText(context, "Refreash is clicked", Toast.LENGTH_SHORT)
                .show();
        RemoteViews remoteViews = mRemoteViews;
        if (mRemoteViews == null) {
            WidgetLog.d(TAG, "mRemoteViews == null");
            remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget_layout);
        }

        final ComponentName cn = new ComponentName(context,
                FilperWidgetProvider.class);
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(context);
//            appWidgetManager.updateAppWidget(appWidgetId, views);
//            remoteViews.setTextViewText(R.id.test1, "Click Test");
        remoteViews.setCharSequence(R.id.test1, "setText", "Test Test");
        remoteViews.setTextViewText(R.id.test2, "Click Test2");
        remoteViews.setTextViewText(R.id.title, "Test");
        int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
        remoteViews.showNext(R.id.viewflipper);
        appWidgetManager.updateAppWidget(id, remoteViews);
        appWidgetManager.updateAppWidget(cn, remoteViews);
    }
    super.onReceive(context, intent);
}
}

正如您所看到的,当我更新Widget标题时,它可以正常工作。但是当我想更新Flipper的子视图(Text1 / Text2)时,它不起作用 - 文本没有改变。 showNext()也正常工作。所以任何人都可以帮忙吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

您需要使用AdapterViewFlipper并在提供RemoteView时使用RemoteViewsService.RemoteViewsFactory进行备份。

这是帮助您入门的sample repo