【问题标题】:How to properly use ListView in AppWidget using Kotlin or Java?如何使用 Kotlin 或 Java 在 AppWidget 中正确使用 ListView?
【发布时间】:2021-08-05 23:43:15
【问题描述】:

我的目标是构建一个可滚动的主屏幕小部件。我正在尝试使用 Kotlin,但也欢迎使用 Java 解决方案。我在网上找到的每个文档要么已经过时,要么不包括滚动。 ScrollView 似乎不支持,所以我尝试使用ListView。据我所知,您只能以编程方式向ListViews 添加内容。我试过了

class NewAppWidget : AppWidgetProvider() {
    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        for (appWidgetId in appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId)
        }
    }
}

internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
    val views = RemoteViews(context.packageName, R.layout.new_app_widget)
    views.addView(R.id.listView, RemoteViews(context.packageName, R.layout.new_app_widget_list))
    appWidgetManager.updateAppWidget(appWidgetId, views)
}

但我的小部件总是如下所示:

【问题讨论】:

  • 你应该使用 recyclerView 而不是 listView 因为 listView 有内存问题

标签: java android kotlin android-widget remoteview


【解决方案1】:

你应该检查官方文件。

https://developer.android.com/guide/topics/appwidgets#collections

您不必以编程方式添加。从 xml 将列表视图添加到布局并设置它

remoteviews.setRemoteAdapter(R.id.yourlistview, intent)

更多信息见上方链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 2019-11-08
    • 2018-09-25
    • 2017-02-19
    相关资源
    最近更新 更多