android RecyclerView:scrollToPosition不工作

6kkfgxo0  于 2023-05-15  发布在  Android
关注(0)|答案(7)|浏览(316)

我有一个显示用户文本消息的简单项目。我目前无法滚动RecyclerView到最后收到的短信。
我的recyclerView是在一个非常简单的活动中使用协调器布局:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/background"
tools:context="com.myproject.myproject.MessageActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_message" />
</android.support.design.widget.CoordinatorLayout>

以及content_message.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.textbutler.textbutler.MessageActivity"
tools:showIn="@layout/activity_message">

<!-- some views here -->

<android.support.v7.widget.RecyclerView
    android:id="@+id/content_text"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scrollbars="vertical" />

<!-- some views here -->
</LinearLayout>

活动通过基本加载器加载数据。我在UI线程上强制访问recyclerview,并有一个延迟(以防万一)

public class MessageActivity extends AppCompatActivity {
private static final int URL_LOADER = 0;

private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private MessageAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    initializeList(savedInstanceState);
}

private void initializeList(Bundle savedInstanceState) {
    if (recyclerView == null) {
        recyclerView = (RecyclerView) findViewById(R.id.content_text);

        if (recyclerView == null)
            return;

        if (layoutManager == null) {
            layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            layoutManager.setReverseLayout(true);
            layoutManager.setStackFromEnd(true);
        }

        recyclerView.setLayoutManager(layoutManager);

        final Activity me = this;

        if (adapter == null) {
            adapter = new MessageAdapter(new ObservableCallback() {
                @Override
                public void callback() {
                    // this function is called right after the recyclerview received notifyDataSetChanged
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            me.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Log.d("ScrollingUI", "scrolltoposition " + adapter.getItemCount());
                                    recyclerView.scrollToPosition(adapter.getItemCount() - 1);
                                }
                            });
                        }
                    }, 1000);
                }
            });
            SMSByUserLoader loader = new SMSByUserLoader(this, adapter);
            getSupportLoaderManager().initLoader(URL_LOADER, savedInstanceState, loader);
        }

        recyclerView.setAdapter(adapter);
    }
}

}
当我运行这段代码时,RecyclerView被完美地填满了。一秒钟后,我有了正确的日志,但视图没有改变。我已经找到了这个问题的各种答案,但似乎没有一个在我的情况下工作。
在RecyclerView中滚动可以完美地工作。

arknldoa

arknldoa1#

我有同样的问题,smoothScrollToPosition方法解决它:

RecyclerView.smoothScrollToPosition(postion);
ego6inou

ego6inou2#

受@omid-jafarinezhad的启发,我需要使用smoothScrollToPosition,但我仍然需要它即时滚动到最后,因为如果继续缓慢滚动许多项目,它看起来会很奇怪。我的解决方法是按顺序使用这两种方法,即:

myRecyclerView.scrollToPosition(position);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                myRecyclerView.smoothScrollToPosition(position);
            }
        }, 50); //sometime not working, need some delay

这可能看起来像一个黑客,但至少解决了我的问题在这个时候。

c86crjj0

c86crjj03#

您使用了layoutManager.setReverseLayout(true)layoutManager.setStackFromEnd(true),以便从末尾开始列表。所以,如果我理解正确的话,列表现在从位置adapter.getItemCount() - 1开始,你的列表不应该滚动。你可以试试...scrollToPosition(0)

fgw7neuy

fgw7neuy4#

我的问题是我的RecyclerView在一个NestedScrollView中。我删除了NestedScrollView,scrollToPosition()工作了。

fhg3lkii

fhg3lkii5#

private val delayShowRunnable by lazy{
    object:Runnable{
        override fun run() {
            rv_content?.alpha=1f
        }
    }
}

private   val  linearSmoothScroller by lazy{
   object: LinearSmoothScroller(context){

         override fun onTargetFound(targetView: View, state: RecyclerView.State, action: Action) {
           super.onTargetFound(targetView, state, action)
           rv_content?.postDelayed(delayShowRunnable,50)
       }
       override fun calculateTimeForDeceleration(dx: Int): Int {
           return  1//set max speed
       }

       override fun calculateTimeForScrolling(dx: Int): Int {
           return 1  //set max speed
       }
    }
}

rv_content?.alpha=0f  //hide listview before scroll finish 
 
 linearSmoothScroller.targetPosition = pos
        (rv_content?.layoutManager as LinearLayoutManager?)?.startSmoothScroll(linearSmoothScroller)

直到下一个布局调用才反映滚动位置改变。所以我们应该使用<smoothscrollToPosition()>,找出目标视图。

bq9c1y66

bq9c1y666#

如果要滚动到适配器recyclerView.scrollToPosition(chatAdapter.getItemCount()-1);的末尾,请始终使用-1

h43kikqp

h43kikqp7#

1.在通常情况下,您可以等待一段时间,直到RecyclerView可以滚动。

binding.recyclerView.postDelayed({
    binding.recyclerView.scrollToPosition(adapter.itemCount - 1)
}, 100)

您可以使用从0到1000的数字,而不是100毫秒,具体取决于设备。
1.如果使用Paging适配器,它可以请求一个新页面。当您尝试滚动RecyclerView时,适配器尚未准备好(尚未添加新项目)。因此,您可以添加一个订阅者来请求完成,然后滚动列表(可能在第一种情况下暂停之后)。

相关问题