RecyclerView:scrollToPosition不起作用

时间:2016-06-05 16:30:44

标签: android scroll android-recyclerview

我有一个显示用户短信的简单项目。我目前无法将RecyclerView滚动到上次收到的短信。
我的recyclelerView使用Coordinator布局进行了一个非常简单的活动:

<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非常有效。

5 个答案:

答案 0 :(得分:6)

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

 RecyclerView.smoothScrollToPosition(postion);

答案 1 :(得分:2)

您使用了layoutManager.setReverseLayout(true)layoutManager.setStackFromEnd(true)来从头开始列表。因此,如果我理解正确,列表现在从位置adapter.getItemCount() - 1开始,您的列表不应滚动。也许你应该试试...scrollToPosition(0)

答案 2 :(得分:2)

我的问题是我的RecyclerView位于 NestedScrollView 中。 我删除了NestedScrollView,然后scrollToPosition()起作用了。

答案 3 :(得分:0)

@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

这似乎是一种黑客行为,但目前至少可以解决我的问题。

答案 4 :(得分:0)

 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)

滚动位置的变化直到下一次布局调用才会反映出来。 所以我们应该使用 ,找出目标视图。