首先,一张图片说了一千个字:
虽然 getViewAt 被称为 4 个项目,但我的光标大小是这样的。
这是代码:
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
System.out.println("Factory");
return(new WidgetViewsFactory(this.getApplicationContext(), intent));
}
}
小部件提供商:
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
}
}
布局:
<?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>
我的 4 个项目肯定没有文本加载,我不知道它来自哪里。
有人可以指出我做错了什么吗?
谢谢
这个答案对我有帮助 https://stackoverflow.com/a/23122006/2051381
我必须从 RemoteViewsFactory 类的 getViewTypeCount() 方法中返回 1 而不是 0
我不得不删除
android:minHeight="?attr/dropdownListPreferredItemHeight"
从列表项的布局来完成这项工作
我来这里也是为了说这个:
我将列表项的背景设置为
?attr/selectableitembackground
如果有人遇到与此相同的问题,并且您使用的是自定义启动器,请尝试使用默认启动器。
另一个想法,在我的案例中有效。确保 item 元素的 XML 有效。我错过了将必需的
android:layout_width
和 android:layout_height
属性添加到列表项视图的元素之一,并实现了获取不确定的“正在加载...”屏幕的确切效果。