小部件 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)
        }
    }
}

它确实正确加载了数据,但它显示的数据完全混乱。 在模拟器上,它以预期的布局开始,然后在我移动列表时立即变得非常混乱。在我用于测试的物理设备中,它从未正确显示,第一项总是有一些非常奇怪的间距。

我认为值得指出的是,当我移动视图时,即使是实际数据也会被弄乱。它不再与真实数据匹配。

帧率有点奇怪,因为我必须去除闪烁并对其进行大量压缩。

enter image description here

如果您需要任何类型的附加信息,我很乐意提供。

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)