Lyn*_*ian 12 android android-widget android-listview
我正在尝试为我的 Android 应用程序构建一个小部件,但遇到了这样的问题:虽然 ListView确实找到了数据(通过调试器,我可以看到 RemoteViewFactory 实现中的 getViewAt 方法被调用并返回正确的 RemoteViews),找到的每个条目都在我的小部件中简单地显示为“正在加载...”
郑重声明:在我的数据库中确实找到了 4 个条目,并且 getViewAt 方法被调用四次,每次都返回一个正确的列表项。
我的代码基于Android 文档,并使用StackView 示例作为技术参考。
在尝试解决这个问题时,我查阅了 stackoverflow 并发现了一些过去的 问题。最常见的修复建议是:
我已尝试了上面的所有建议,并且仅使用支持的视图,但我的项目仍然没有显示。这里有人有想法吗?
我编写的相关代码列表:
添加到我的 AndroidManifest.xml 文件中的条目
<service
android:name=".widget.DfdWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<receiver android:name=".widget.DfdWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/dfd_widget_info" />
</receiver>
Run Code Online (Sandbox Code Playgroud)
小部件布局 xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80DDE8ED"
android:padding="@dimen/widget_margin">
<ListView
android:id="@+id/widgetRemindersListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/dfd_widget_list_item">
</ListView>
<TextView
android:id="@+id/widgetEmptyViewText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="You have no reminders for today, nice!"
android:textAlignment="center"
android:textSize="20sp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
小组件列表项 xml
(作为测试,我确实尝试删除除 TextView 之外的所有内容,看看 Android 是否没有发现 ImageView 违规。这并不重要。)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widgetListItem"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/widgetCompletedCheckBox"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:layout_marginStart="10dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:srcCompat="@android:drawable/checkbox_off_background" />
<TextView
android:id="@+id/widgetReminderName"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_toEndOf="@id/widgetCompletedCheckBox"
android:text="Reminder name"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="@+id/widgetReminderDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/widgetReminderName"
android:layout_toEndOf="@id/widgetCompletedCheckBox"
android:text="Ma 1 jan"
android:textColor="#2196F3" />
<ImageView
android:id="@+id/widgetReminderRepeating"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@id/widgetReminderName"
android:layout_toEndOf="@id/widgetReminderDate"
android:background="#00FFFFFF"
app:srcCompat="@android:drawable/ic_menu_revert"
tools:visibility="invisible" />
<ImageView
android:id="@+id/widgetIsImportant"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
app:srcCompat="@android:drawable/btn_star_big_off" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
小部件提供者类
public class DfdWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Intent intent = new Intent(context, DfdWidgetService.class);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.dfd_widget);
remoteViews.setRemoteAdapter(R.id.widgetRemindersListView, intent);
remoteViews.setEmptyView(R.id.widgetRemindersListView, R.id.widgetEmptyViewText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
@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);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
@Override
public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
super.onRestored(context, oldWidgetIds, newWidgetIds);
}
}
Run Code Online (Sandbox Code Playgroud)
小组件服务类
public class DfdWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new DfdRemoteViewsFactory(getApplicationContext());
}
class DfdRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private final Context context;
private ReminderRepository reminderRepository;
private List<Reminder> reminders;
public DfdRemoteViewsFactory(Context context) {
this.context = context;
this.reminderRepository = ReminderRepository.getReminderRepository(context);
}
@Override
public void onCreate() {
reminders = reminderRepository.getAllRemindersForTodayList();
}
@Override
public void onDataSetChanged() {
reminders = reminderRepository.getAllRemindersForTodayList();
}
@Override
public void onDestroy() {
reminders.clear();
}
@Override
public int getCount() {
return reminders.size();
}
@Override
public RemoteViews getViewAt(int position) {
Reminder reminder = reminders.get(position);
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.id.widgetListItem);
remoteView.setTextViewText(R.id.widgetReminderName, reminder.getName());
// Removed code that sets the other fields as I tried it with and without and it didn't matter. So removed for brevity
return remoteView;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
return reminders.get(position).getId();
}
@Override
public boolean hasStableIds() {
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.id.widgetListItem)
Run Code Online (Sandbox Code Playgroud)
RemoteViews您尝试使用的构造函数将布局资源 ID 作为第二个参数。您正在传递一个小部件 ID。
因此,替换R.id.widgetListItem为R.layout.whateverYourLayoutNameIsForTheListItems,其中您可以替换whateverYourLayoutNameIsForTheListItems为列表项的布局名称。
| 归档时间: |
|
| 查看次数: |
2280 次 |
| 最近记录: |