scrollToPositionWithOffset не работает с Android WearableRecyclerView - PullRequest
0 голосов
/ 25 мая 2018

Я не могу на всю жизнь заставить свое приложение программно прокручивать до позиции в пригодном для носки повторном просмотре.Я пробовал следующее:

  1. Наблюдение за onGlobalLayout и прокрутка
  2. Добавление кнопки и прокрутка при нажатии кнопки (на случай, если что-то происходит в том месте, где был списокне полностью загружен или перерисовывается)
  3. Последнее усилие по использованию канавы с использованием событийной шины Greenrobot, чтобы убедиться, что это произошло в главном потоке пользовательского интерфейса, все еще без прокрутки

Вот мой макет:

<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    app:layout_box="left|bottom|right">
    <Button
        android:id="@+id/gotolast_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Go to last"/>
    <android.support.wear.widget.WearableRecyclerView xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/listview"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        tools:listitem="@layout/item_string" />

</LinearLayout>

вот мой код:

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    // TRYING TO DO IT WITH A MANUAL BUTTON CLICK
    gotolast_button.setOnClickListener {  
        val lastValue = PreferenceManager.getDefaultSharedPreferences(context).getInt(WearMainActivity.PREF_LAST_VALUE, 0)
        (listview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(lastValue,0)
    }

    // TRYING TO DO IT WITH GLOBALLAYOUTLISTENER
    listview.adapter = StringListAdapter(getList()) 
    listview.viewTreeObserver.addOnGlobalLayoutListener ( object:ViewTreeObserver.OnGlobalLayoutListener{
        override fun onGlobalLayout() {
            if (listview.childCount > 0) {
                // scroll to the last selected grade
                val lastValue = PreferenceManager.getDefaultSharedPreferences(context).getInt(WearMainActivity.PREF_LAST_VALUE, 0)
                (listview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(lastValue,0)
                listview.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        }
    })
}

Ничего не работает.Любая помощь будет высоко ценится !!

1 Ответ

0 голосов
/ 01 июня 2018

Вы можете попытаться выполнить scrollToPositionWithOffset внутри исполняемого файла внутри обработчика, подобного этому

Handler().postDelayed({
   (listview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(lastValue,0)
}, 200).
...