Код: Выделить всё
internal fun updateTodoWidget(
context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int
) {
val views = RemoteViews(context.packageName, R.layout.todo_widget)
views.setRemoteAdapter(
R.id.list, Intent(context, WidgetService::class.java).apply {
putExtra("appWidgetId", appWidgetId)
}
)
views.setTextViewText(
R.id.count, (context.readSharedPreference("todoCount", 0) as Int).toString()
)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
Код: Выделить всё
class WidgetService : RemoteViewsService() {
override fun onGetViewFactory(intent: Intent?): RemoteViewsFactory {
return DataProvider(this, intent)
}
}
Код: Выделить всё
class DataProvider(widgetService: WidgetService, val intent: Intent?) : RemoteViewsService.RemoteViewsFactory {
private var myListView: MutableList = ArrayList()
private var mContext: Context? = null
override fun getViewAt(position: Int): RemoteViews {
val view = RemoteViews(mContext!!.packageName, R.layout.todo_item_widget)
view.setTextViewText(R.id.item_text, myListView[position].name)
view.setOnClickFillInIntent(
R.id.done, Intent(mContext, ActionReceiver::class.java).apply {
action = "doneTodo"
putExtra("id", myListView[position].id)
putExtra("appWidgetId", intent?.getIntExtra("appWidgetId", 69) ?: 69)
}
)
// view.setOnClickPendingIntent(
// R.id.done,
// PendingIntent.getBroadcast(
// mContext,
// 85,
// Intent(mContext,ActionReceiver::class.java).apply {
// action = "doneTodo"
// putExtra("id",myListView[position].id)
// putExtra("appWidgetId",intent?.getIntExtra("appWidgetId",69)?:69)
// },
// PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
// )
// )
return view
}
}
Код: Выделить всё
Я экспериментировал с различными намерениями, например, намерения действия, но кажется, что элементы ListView недоступны для кликов.
Подробнее здесь: https://stackoverflow.com/questions/788 ... remoteview
Мобильная версия