当我把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);
}
}
如您所见,当我更新小部件标题时,它可以工作。但是,当我想要更新Flipper的子视图(Text1/Text2)时,它不起作用-文本没有更改。 showNext()
也能正常工作。请问有人能为此提供帮助吗?提前致谢。