RecyclerView:scrollToPosition无法正常工作 [英] RecyclerView: scrollToPosition not working

查看:76
本文介绍了RecyclerView:scrollToPosition无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示用户短信的简单项目.我目前无法将RecyclerView滚动到最后收到的短信.
我的recyclerView在使用Coordinator布局的一个非常简单的活动中:

I have a simple project displaying user text messages. I am currently not able to scroll the RecyclerView to the last received text message.
My recyclerView is inside a very simple activity using the Coordinator layout:

<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:

And the 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并有延迟(以防万一)

The activity load the data through a basic loader. I force the access to the recyclerview on the UI thread and with a delay (just in case)

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中滚动完全可以正常工作.

When I run this code, the RecyclerView is perfectly filled. After a second I have the correct log but the view does not changed. I already found various answers to this issue, but none of them seems to work in my case.
Scrolling in the RecyclerView perfectly works.

推荐答案

您使用了layoutManager.setReverseLayout(true)layoutManager.setStackFromEnd(true)以便从头开始创建列表.因此,如果我理解正确,那么列表现在从位置adapter.getItemCount() - 1开始,并且您的列表不应滚动.也许您应该尝试...scrollToPosition(0).

You used layoutManager.setReverseLayout(true) and layoutManager.setStackFromEnd(true) in order to start your list from the end. So, if I understand it correctly, the list starts now from position adapter.getItemCount() - 1 and your list shouldn't scroll. Maybe you should try ...scrollToPosition(0).

这篇关于RecyclerView:scrollToPosition无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆