简体   繁体   English

scrollToPositionWithOffset不适用于android WearableRecyclerView

[英]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: 我尝试了以下方法:

  1. Watching for onGlobalLayout and scrolling 注意onGlobalLayout和滚动
  2. Adding a button and scrolling when the button was clicked (just in case there was something going on where the list wasn't fully loaded or was redrawing) 添加一个按钮并在单击该按钮时滚动(以防万一在列表未完全加载或正在重新绘制列表的情况下发生某些事情)
  3. Last ditch effort using Greenrobot eventbus to make sure it happened on the main UI thread, still no scrolling 最后一次使用Greenrobot eventbus的努力,以确保它发生在主UI线程上,但仍然没有滚动

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.

相关问题 WearableRecyclerView.scrollToPosition 不适用于磨损? - WearableRecyclerView .scrollToPosition not working for wear? WearableRecyclerView Android Wear OS - WearableRecyclerView Android Wear OS WearableRecyclerView OffsettingHelper.updateChild()不起作用 - WearableRecyclerView OffsettingHelper.updateChild() not working LinearLayoutManager#scrollToPositionWithOffset()有时无法正常工作 - LinearLayoutManager#scrollToPositionWithOffset() not working sometimes RecyclerView 上 LinearLayoutManager 的 scrollToPositionWithOffset 不起作用 - scrollToPositionWithOffset from LinearLayoutManager on RecyclerView not working 如果RecyclerView太短,则Android LinearLayoutManager scrollToPositionWithOffset无法正常工作 - android LinearLayoutManager scrollToPositionWithOffset not work if RecyclerView is too short RecyclerView scrollToPositionWithOffset与动画 - RecyclerView scrollToPositionWithOffset with animation Kotlin/RecyclerView:scrollToPositionWithOffset 未显示 - Kotlin/RecyclerView: scrollToPositionWithOffset Not Showing Up Kotlin中未解决的参考WearableRecyclerView.Adapter错误 - Unresolved reference WearableRecyclerView.Adapter error in Kotlin RecyclerView如何设置scrollToPositionWithOffset(x,y)? - How to set the scrollToPositionWithOffset(x,y) of RecyclerView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM