4

I have problems about updating child view in ViewFlipper when I use the ViewFlipper as the Widget.

Here are layout for Widgets:

<?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>

And Here is the 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);
}
}

As you can see, when I update the Widget Title, it can work. But When I want to update the subView of Flipper (Text1/Text2), It doesn't work-The text is not changed. The showNext() also work correctly. So anyone can help for that? Thanks in advance.

buptcoder
  • 2,692
  • 19
  • 22

1 Answers1

3

You need to use AdapterViewFlipper and back it up with RemoteViewsService.RemoteViewsFactory as you are supplying with RemoteViews .

Here is a sample repo to help you get started.

AndDev
  • 31
  • 4