小部件中的GridView无限加载

时间:2018-04-10 15:26:14

标签: android gridview widget

我想在gridview中显示我的小部件上的图像。目前我有树元素,我只看到这无限加载图像。可能是什么原因?

gridview on widget loading

这是我的代码。

@Override
public RemoteViews getViewAt(int position) {

    ...
    RemoteViews views = new RemoteViews(mContext.getPackageName(),R.layout.plant_widget);
    int imgRes = PlantUtils.getPlantImageRes(mContext,
            currentTime - createdAt, currentTime - wateredAt, plantType);
    views.setImageViewResource(R.id.widget_plant_image, imgRes);
    views.setTextViewText(R.id.widget_plant_name, Long.toString(plantId));

    return views;
}

private static RemoteViews getGardenGridRemoteView(Context context, int imgRes, long plantId, boolean showWater)
{
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_grid_view);
    Intent intent = new Intent(context, GridWidgetService.class);
    views.setRemoteAdapter(R.id.gv_widget_plant, intent);
    Intent appIntent = new Intent(context, PlantDetailActivity.class);
    PendingIntent appPendingIntent = PendingIntent.getActivity(
            context, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    views.setPendingIntentTemplate(R.id.gv_widget_plant, appPendingIntent);

    views.setEmptyView(R.id.gv_widget_plant, R.id.empty_view);

    return views;
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我能够通过确保RemoteViewsFactory类中的getViewTypeCount()返回非零值来解决此问题。以下应该可以解决问题:

@Override
public int getViewTypeCount() {
    return 1; // if this is 0, then loading will be "perpetual"
}