[英]scrollToPositionWithOffset not working with android WearableRecyclerView
I cannot for the life of me get my app to programmatically scroll to a position in a wearablerecyclerview. 我一生都无法让我的应用以编程方式滚动到可穿戴式回收器视图中的某个位置。 I've tried the following: 我尝试了以下方法:
Here's my layout: 这是我的布局:
<?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>
here's my code: 这是我的代码:
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)
}
}
})
}
Nothing worked. 没事。 Any help would be much appreciated!! 任何帮助将非常感激!!
You could try to execute scrollToPositionWithOffset within a runnable inside a handler like this 您可以尝试在这样的处理程序内的runnable中执行scrollToPositionWithOffset
Handler().postDelayed({
(listview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(lastValue,0)
}, 200).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.