Kotlin-集合小部件无法加载项目

时间:2019-07-17 13:39:33

标签: android-studio listview kotlin widget

我有一个使用ListView的主屏幕小部件-当前每个项目都显示默认的“正在加载...”。另外,当小部件在主屏幕上处于活动状态时,该应用在启动时崩溃。

我已阅读并遵循: https://www.vogella.com/tutorials/AndroidWidgets/article.html#collection-view-widgets https://github.com/udacity/android-widget/blob/master/app/src/main/res/layout/collection_widget.xml https://dharmangsoni.blogspot.com/2014/03/collection-widget-with-event-handling.html https://laaptu.wordpress.com/2013/07/19/android-app-widget-with-listview/

加上官方文档和堆栈视图演示

我已经阅读了有关该主题的所有堆栈溢出答案-大多数人只需要删除自定义视图,不受支持的元素属性或在getViewTypeCount中返回1,但是这些都不适合我。

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.stickynotes">

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <receiver android:name=".StickyNotesWidget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>

            <meta-data
                    android:name="android.appwidget.provider"
                    android:resource="@xml/sticky_notes_widget_info"/>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <service android:name="com.example.stickynotes.StickyNotesWidgetRemoteViewsService"
            android:permission="android.permission.BIND_REMOTEVIEWS">
        </service>
    </application>

</manifest>

widget_collection_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">


    <ListView android:layout_width="match_parent" android:layout_height="wrap_content"
              android:id="@+id/widget_list_view"
              />

    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
              android:id="@+id/widget_empty_view"
              android:text="EMPTY"/>

</LinearLayout>

widget_list_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/widget_list_item">
</TextView>

窗口小部件提供程序类:

override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        val view = RemoteViews(context.packageName, R.layout.widget_collection_layout)

        val intent = Intent(context, StickyNotesWidgetRemoteViewsService::class.java)
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
        intent.data = Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))

        view.setRemoteAdapter(R.id.widget_list_view, intent)
        view.setEmptyView(R.id.widget_list_view, R.id.widget_empty_view)

        appWidgetManager.updateAppWidget(appWidgetId, view)
}

小部件服务类:

class StickyNotesWidgetRemoteViewsService : RemoteViewsService() {
    override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
        return StickyNotesWidgetRemoteViewsFactory(applicationContext, intent)
    }
}

Widget RemoteViewsFactory实现:

class StickyNotesWidgetRemoteViewsFactory(ctx: Context, intent: Intent) : RemoteViewsService.RemoteViewsFactory {
    private val context = ctx
    private var collection = listOf("1", "2", "3", "4", "5")

    override fun onCreate() { collection = listOf("1", "2", "3", "4", "5") }
    override fun onDataSetChanged() { collection = listOf("1", "2", "3", "4", "5") }
    override fun onDestroy() {}
    override fun getCount(): Int { return collection.size }
    override fun getLoadingView(): RemoteViews? { return null }
    override fun getViewTypeCount(): Int { return 1 }
    override fun getItemId(p0: Int): Long { return p0 as Long }
    override fun hasStableIds(): Boolean { return true }

    override fun getViewAt(p0: Int): RemoteViews {
        val views = RemoteViews(context?.packageName, R.layout.widget_list_item)
        views.setTextViewText(R.id.widget_list_item, collection[p0])
        return views
    }

}

我希望看到五个列表项,这些列表项根据工厂类中的字符串列表进行了编号。

虽然我确实有五个列表项,但它们都显示为“正在加载...”。

我错过了什么?谢谢!

0 个答案:

没有答案