如何解决小部件 ListView
我从未在 Android 中使用过小部件,所以我很确定我正在做很多愚蠢的事情。尽管如此,我无法弄清楚为什么这不起作用。我有一个 WidgetConfigureActivity,它允许用户选择要在该特定小部件中显示的一些数据。在小部件内部,我需要一个 ListView,我通过 RemoteViewsService.RemoteViewsFactory 类扩展来处理它。
我知道这个适配器一团糟,但我不明白为什么它不应该工作。
class PairingWidgetService : RemoteViewsService() {
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
return PairingWidgetDataProvider(this,intent)
}
}
class PairingWidgetDataProvider(val context: Context,val intent: Intent) :
RemoteViewsService.RemoteViewsFactory {
val database = getPairingsDatabase(context)
val repository = PairingsRepository(database)
var offspringList: List<Speciment>
var offspringListViews: List<RemoteViews> = listof()
private val oddsOutOf: Int
get() {
var odds = 0
offspringList.forEach {
odds += it.incidence
}
return odds
}
private val widgetId: Int
val oddsMode: Int
val coroutinescope: Coroutinescope
init {
offspringList = listof()
offspringListViews = listof()
val defPref = PreferenceManager.getDefaultSharedPreferences(context)
oddsMode = defPref.getString(OddsdisplayStyle.PREF_KEY,null)?.toInt()
?: OddsdisplayStyle.PERCENTAGE
widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,-1)
coroutinescope = Coroutinescope(Job() + dispatchers.IO)
}
override fun onCreate() { }
override fun onDataSetChanged() {
loadData()
}
override fun onDestroy() {
coroutinescope.cancel()
}
override fun getCount(): Int = offspringListViews.size
override fun getViewAt(position: Int): RemoteViews = offspringListViews[position]
override fun getLoadingView(): RemoteViews? {
return null
}
override fun getViewTypeCount(): Int = 1
override fun getItemId(position: Int): Long = position.toLong()
override fun hasstableIds(): Boolean = true
fun loadData() {
offspringList = listof()
offspringListViews = listof()
if (widgetId == -1) return
val widgetSharedPref =
context.getSharedPreferences(PairingWidgetConfigureActivity.PREFS_NAME,0)
val pairingId =
widgetSharedPref.getInt(PairingWidgetConfigureActivity.PREF_PREFIX_KEY + widgetId,-1)
offspringList = repository.getPairing(pairingId).offspringList
offspringListViews = offspringList.map {
getRemoteViewsFor(context,it,oddsOutOf,oddsMode)
}
}
}
它确实正确加载了数据,但它显示的数据完全混乱。 在模拟器上,它以预期的布局开始,然后在我移动列表时立即变得非常混乱。在我用于测试的物理设备中,它从未正确显示,第一项总是有一些非常奇怪的间距。
我认为值得指出的是,当我移动视图时,即使是实际数据也会被弄乱。它不再与真实数据匹配。
帧率有点奇怪,因为我必须去除闪烁并对其进行大量压缩。
如果您需要任何类型的附加信息,我很乐意提供。
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。