【发布时间】:2021-11-13 17:27:14
【问题描述】:
我创建了一个 android 应用程序,我正在尝试添加一个基本小部件。在我的实体手机上,小部件从不填充,而在 我正在使用启动器的虚拟设备“一直在停止” 小部件将可见(当我的主应用程序可见时很好 - 这也可能是因为 MyWidgetProvider OnUpdate 仅在主应用程序的 onPause() 上触发 - 而不是任何时间间隔)。
我正在尝试关注 this 使用远程填充 ListView 小部件 意见。
我用 TextView 代替了 ListView,但我想要额外的格式并内置 ListView 小部件的垂直滚动。
在调试时,我看不到任何问题,但很明显有些问题 在某个地方,我只能想象它在 onUpdate 中出错了 方法。
任何人都可以看出哪里出了问题,或者有什么想法可以解决问题吗? 调试?任何线索表示赞赏!
MyWidgetProvider.java
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate (Context context,
AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Attach the remote adaptor to each of the widgets
for (int appWidgetId : appWidgetIds) {
Log.d("LOG", "Updating widget " + appWidgetId);
// Create the intent which references the list widget
// service.
Intent intent = new Intent(context, MyListWidgetService.class);
RemoteViews rv = new RemoteViews(context.getPackageName(),
R.layout.appwidget);
rv.setRemoteAdapter(R.id.widgetListView, intent);
appWidgetManager.updateAppWidget(appWidgetId, rv);
}
}
}
MyListWidgetService.java
public class MyListWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new MyRemoteViewsFactory(this.getApplicationContext());
}
}
class MyRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
public MyRemoteViewsFactory(Context context) {
mContext = context;
}
public void onCreate() {
}
public void onDestroy() {
}
public int getCount() {
return 4;
}
public RemoteViews getViewAt(int position) {
RemoteViews rv;
int val;
rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_list_item_view);
// Dummy values
rv.setTextViewText(R.id.widgetItemA, "Dummy Text 1");
rv.setTextViewText(R.id.widgetItemB, "Dummy Text 2");
val = (position % 3);
if (val != 0) {
rv.setTextViewText(R.id.widgetItemC, "(" + val + ")");
}
return rv;
}
public RemoteViews getLoadingView() {
return null;
}
public int getViewTypeCount() {
return 1;
}
public long getItemId(int position) {
return position;
}
public boolean hasStableIds() {
return true;
}
public void onDataSetChanged() {
}
}
appwidget.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/widgetListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded_rect"
android:fadeScrollbars="true" />
</LinearLayout>
widget_list_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/widgetItemA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/padding"
android:layout_marginTop="@dimen/padding" />
<TextView
android:id="@+id/widgetItemB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding" />
<TextView
android:id="@+id/widgetItemC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding"
android:layout_marginEnd="@dimen/padding" />
</LinearLayout>
【问题讨论】:
标签: java android android-listview android-widget android-remoteview