主屏幕小部件,listView显示"正在加载"

Mav*_*ick 13 android homescreen android-widget

首先,图像说千言万语:

在此输入图像描述

虽然getViewAt被称为4项,但是我的光标大小是这样的.

这是代码:

public class WidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        System.out.println("Factory");
        return(new WidgetViewsFactory(this.getApplicationContext(), intent));
    }
}
Run Code Online (Sandbox Code Playgroud)

小部件提供者:

public class WidgetViewsFactory implements RemoteViewsService.RemoteViewsFactory, LNTConstants
{   
    private Context context = null;

    ArrayList<DBItemModel> items;

    public WidgetViewsFactory(Context context, Intent intent) {
        this.context = context;

        Cursor c = Items.get(context, where);
        items = Items.getFromCursor(c);
    }

    @Override
    public void onCreate() {
        /*

        */
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public int getCount() {
        if(items != null) {
            System.out.println("Count: " + items.size());
            return items.size();
        }
        return 0;
    }

    @Override
    public RemoteViews getViewAt(int position) {
        System.out.println("getViewAt: " + position);
        ItemBean item = items.get(position);

        RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.item_list);
        row.setTextViewText(R.id.tvListItem, item.summary);

        System.out.println("Widget item title: " + item.summary);

        return row;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public void onDataSetChanged() {
        // no-op
    }
}
Run Code Online (Sandbox Code Playgroud)

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:id="@+id/tvListItem"
        style="@style/text_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:minHeight="?attr/dropdownListPreferredItemHeight" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我的4个项目肯定没有文本加载,我不知道它来自哪里.

有人能指出你做错了什么吗?

谢谢

Chu*_*y47 18

这个答案帮助了我 /sf/answers/1618540451/

我必须从我的remoteViewsFactory类的getViewTypeCount()方法返回1而不是0


hum*_*zed 13

检查您是否使用不受支持的视图.

在我的情况下,我View在我的布局中使用分隔符.并没有意识到它不是受支持的观点之一.

我刚删除它,小部件按预期运行.

  • 我知道这一点,但我从回收视图复制了布局,并没有考虑不支持视图。 (2认同)

Mav*_*ick 8

我不得不删除

android:minHeight="?attr/dropdownListPreferredItemHeight"
Run Code Online (Sandbox Code Playgroud)

从列表项的布局来使这项工作