我的应用存在一些问题。我从内容提供者那里获取了一些图像,并试图在stackview小部件中显示它们,但是无法单击stackview小部件,它只显示很少的图像(与小部件尺寸不匹配)。 这是小部件的屏幕截图:
这是我的窗口小部件提供程序类:
public class FavMovieWidget extends AppWidgetProvider {
public static final String TOAST_ACTION = "TOAST_ACTION";
public static final String EXTRA_ITEM = "EXTRA_ITEM";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; ++i) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.fav_movie_widget);
Intent intent = new Intent(context, FavMovieService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(R.id.stack_view, intent);
views.setEmptyView(R.id.stack_view, R.id.empty_view);
Intent toastIntent = new Intent(context, FavMovieWidget.class);
toastIntent.setAction(FavMovieWidget.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
toastIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.stack_view, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], views);
}
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
}
@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 appWidgetManager = 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);
}
}
远程查看服务类:
public class FavMovieService extends RemoteViewsService {
public FavMovieService() {
}
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new RemoteViewFactory(this.getApplicationContext(),intent);
}
@Override
public void onCreate() {
super.onCreate();
}
}
这是我的视图工厂类:
public class RemoteViewFactory implements RemoteViewsService.RemoteViewsFactory {
private List<Bitmap> widgetItem = new ArrayList<>();
private Context context;
private int appWidgetId;
Cursor cursor;
public RemoteViewFactory(Context context, Intent intent){
this.context = context;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
@Override
public void onCreate() {
cursor = context.getContentResolver().query(DatabaseContract.CONTENT_URI, null, null, null, null);
}
@Override
public void onDataSetChanged() {
final long identityToken = Binder.clearCallingIdentity();
cursor = context.getContentResolver().query(DatabaseContract.CONTENT_URI, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
String crsPoster = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseContract.TableColumns.POSTER));
try {
Bitmap bitmap = Glide.with(context)
.load("http://image.tmdb.org/t/p/w92/"+crsPoster)
.asBitmap()
.error(new ColorDrawable(context.getResources().getColor(R.color.colorAccent)))
.into(180,250).get();
widgetItem.add(bitmap);
}
catch (InterruptedException|ExecutionException e){
Log.d("Widget Load Error","error");
}
cursor.moveToNext();
}
while (!cursor.isAfterLast());
}
Binder.restoreCallingIdentity(identityToken);
}
@Override
public void onDestroy() {
if (cursor!=null){
cursor.close();
}
}
@Override
public int getCount() {
return widgetItem.size();
}
@Override
public RemoteViews getViewAt(int i) {
Bitmap bitmap = null;
RemoteViews rv = new RemoteViews(context.getPackageName(),
R.layout.widget_item);
cursor.moveToPosition(i);
MovieItem movieItem = new MovieItem(cursor);
try {
bitmap = Glide.with(context)
.load("http://image.tmdb.org/t/p/w92/"+movieItem.getMoviePoster())
.asBitmap()
.error(new ColorDrawable(context.getResources().getColor(R.color.colorAccent)))
.into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get();
}catch (InterruptedException|ExecutionException e){
Log.d("Widget Load Error","error");
}
rv.setImageViewBitmap(R.id.iv_poster,bitmap);
Bundle extras = new Bundle();
extras.putInt(FavMovieWidget.EXTRA_ITEM,i);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.iv_poster,fillInIntent);
return rv;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int i) {
return cursor.moveToPosition(i) ? cursor.getLong(0) : i;
}
@Override
public boolean hasStableIds() {
return true;
}
}
这是小部件xml文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<StackView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/stack_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:loopViews="true"
android:layout_weight="9" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/banner_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_gravity="center"
android:background="@color/colorPrimaryDark"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="@string/appwidget_text"
android:textSize="20sp"
android:layout_weight="1" />
</LinearLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:layout_gravity="center"
android:background="@color/colorAccent"
android:textColor="#ffffff"
android:text="@string/empty_text"
android:visibility="gone" />