1

我有一个RecyclerView我用来以文本应用程序类型的方式显示消息的方法。我的意思是,添加到的新消息RecyclerView应该出现在底部。

目前,我正在使用.setStackFromEnd(true)LinearLayoutManager的消息从底部开始显示:

layMng = new LinearLayoutManager(this);
layMng.setStackFromEnd(true);

mRecyclerView.setLayoutManager(layMng);
mRecyclerView.setAdapter(mAdapter);

发生的情况是,当收到足够多的消息并且消息列表即将溢出屏幕顶部时,它会停止始终在底部显示新消息。我必须手动向下滚动才能看到新增内容。

如何让它始终可以查看最新消息或在RecyclerView收到新项目时向下滚动?

我在我的布局管理器上尝试了各种方法.scrollToPosition().scrollToPositionWithOffset()但到目前为止我看到的所有建议都没有奏效。关于该问题的一些顶级 StackOverflow 问题的最佳答案甚至无法解决原始问题。

那么这是怎么做到的呢?

4

4 回答 4

1

请检查 layout_height。它应该是 android:layout_height="match_parent"

在 UI 线程中运行 smoothScrollToPosition

我的代码中的代码片段

            <android.support.v7.widget.RecyclerView
                android:id="@+id/topic_screen_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="18dp"

                />

//这里 topicScreenList 引用了我的 Recylerview: //-messageAdapter: 引用适配器对象

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    layoutManager.setStackFromEnd(true);
    topicScreenList.setLayoutManager(layoutManager);
    topicScreenList.setAdapter(messageAdapter);
}

收到任何新消息后,从后端服务调用以下代码

- 这里 MessageItem 引用适配器使用的新消息项

@Override
public void onNewMessage(final MessageItem messageItem) {
    if (getActivity() != null) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {                  
               messageAdapter.addMessageToEnd(messageItem);

       topicScreenList.smoothScrollToPosition(messageAdapter.getItemCount());

            }
        });
    }
}


//code from my Adapter  
public void addMessageToEnd(MessageItem messageItem) {
    messageItems.add(messageItem);
    notifyItemInserted(messageItems.size() -1);
}
于 2017-11-19T00:38:24.953 回答
0

试试这个,我也在开发一个聊天应用程序,它对我来说非常有用

    mRecyclerView.setAdapter(adaptorChat);
    mRecyclerView.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    llm.setStackFromEnd(true);
    mRecyclerView.setLayoutManager(llm);

XML

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/messages_list_sponsor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/edit_compose_msg_container" />

    <LinearLayout
        android:id="@+id/edit_compose_msg_container"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        android:padding="3dp">

        <EditText
            android:id="@+id/compose_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_weight="1"
            android:background="@drawable/white_edit_text_style"
            android:hint="@string/enter_message"
            android:inputType="textMultiLine"
            android:minHeight="50dp"
            android:paddingEnd="4dp"
            android:paddingLeft="6dp"
            android:paddingRight="4dp"
            android:paddingStart="6dp"
            android:singleLine="false"
            android:textColor="@color/black"
            android:textColorHint="@android:color/darker_gray" />

        <ImageView
            android:id="@+id/send_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="5"
            android:clickable="true"
            android:padding="15dp"
            android:rotation="-35"
            android:layout_marginBottom="5dp"
            android:src="@drawable/message_send" />
    </LinearLayout>
</RelativeLayout>
于 2016-06-23T04:15:24.737 回答
0

mRecyclerView.scrollToPosition(position);应该管用。可能是它在更新位置/适配器之前尝试滚动。尝试:

int delay = 500;

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        //scroll to bottom
        mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount());
    }
}, delay);
于 2016-06-23T05:55:51.320 回答
0

如果你设置了 mRecyclerView.setNestedScrollingEnabled(false)然后删除它。

于 2019-08-12T13:00:56.547 回答