我试图用提供的图像(使用滑行从互联网加载的图像)和文本制作stackwidget。这两个值都存在,但是当我尝试将其分配给像这样的rv.setImageViewBitmap(R.id.img_widget,bmp);
时,它什么也没显示。
这是WidgetFactory的代码
private MovieFavorite getFav(int position) {
if (!cursor.moveToPosition(position)) {
throw new IllegalStateException("Position invalid!");
}
return new MovieFavorite(cursor.getString(cursor.getColumnIndexOrThrow(
MovieContract.MovieColumns.MOVIE_ID)),
cursor.getString(cursor.getColumnIndexOrThrow(MovieContract.MovieColumns.MOVIE_IMAGE)),
cursor.getString(cursor.getColumnIndexOrThrow(MovieContract.MovieColumns.MOVIE_TITLE)));
}
@Override
public void onCreate() {
cursor = mContext.getContentResolver().query(
MovieContract.CONTENT_URI,
null,
null,
null,
null
);
}
@Override
public void onDataSetChanged() {
if (cursor != null) {
cursor.close();
}
final long identityToken = Binder.clearCallingIdentity();
cursor = mContext.getContentResolver().query(
MovieContract.CONTENT_URI, null, null, null, null);
Binder.restoreCallingIdentity(identityToken);
}
@Override
public void onDestroy() {
if (cursor != null) {
cursor.close();
}
}
@Override
public int getCount() {
return cursor.getCount();
}
@Override
public RemoteViews getViewAt(int position) {
MovieFavorite movieFavorite = getFav(position);
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.movie_widget);
Log.d("Widgetku",movieFavorite.getTitle());
Bitmap bmp = null;
try {
bmp = Glide.with(mContext)
.asBitmap()
.load(UtilsConstant.BASE_BACKDROP_URL_WIDGET+movieFavorite.getImage())
.into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();
rv.setImageViewBitmap(R.id.img_widget,bmp);
Log.d("Widgetku","Yessh");
}catch (InterruptedException | ExecutionException e){
Log.d("Widget Load Error","error");
}
rv.setTextViewText(R.id.tv_movie_title, movieFavorite.getTitle());
Bundle extras = new Bundle();
extras.putInt(MovieWidget.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.img_widget, fillInIntent);
return rv;
}
这是窗口小部件服务的代码
public class MovieWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new MovieRemoteViewsFactory(this.getApplicationContext(), intent);
}
@Override
public void onCreate() {
super.onCreate();
}
}
最后是Widget类的代码
`
public static void updateAppWidget(上下文上下文,AppWidgetManager,appWidgetManager, int appWidgetId){
Intent intent = new Intent(context, MovieWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.movie_widget);
views.setRemoteAdapter(R.id.stack_view, intent);
views.setEmptyView(R.id.stack_view, R.id.empty_view);
Intent toastIntent = new Intent(context, MovieWidget.class);
toastIntent.setAction(MovieWidget.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
if (intent.getAction().equals(TOAST_ACTION)) {
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
在此先感谢您的帮助!