Java Recyclerview在更新时移动到底部

Java Recyclerview在更新时移动到底部,java,android,xml,android-recyclerview,Java,Android,Xml,Android Recyclerview,您好,我正在创建聊天应用程序。应用程序运行良好。但是,每当收到新消息时(通过后台服务),iam都会更新recylerview,然后用户必须向下滚动以查看新消息 我想做的是,当收到新消息时,我想更新recyclerview并滚动到底部。这样,用户无需滚动即可看到最后收到的消息 聊天活动 public class Chatting extends AppCompatActivity { private DrawerLayout drawerLayout; private Appl

您好,我正在创建聊天应用程序。应用程序运行良好。但是,每当收到新消息时(通过后台服务),iam都会更新recylerview,然后用户必须向下滚动以查看新消息

我想做的是,当收到新消息时,我想更新recyclerview并滚动到底部。这样,用户无需滚动即可看到最后收到的消息

聊天活动

public class Chatting extends AppCompatActivity {


    private DrawerLayout drawerLayout;
    private ApplicationEnvironmentURL applicationEnvironment;
    private Toolbar toolbar;
    private SelectVisitorService mService = new SelectVisitorService();
    private boolean mBound = false;
    public static Context baseContext;

    private static String  uniqueID;
    public static ChattingAdapter mAdapter ;
    public static List<ChattingItomObject> messageItems = new ArrayList<ChattingItomObject>();
    private RecyclerView recyclerView;

    public String ProfileId;
    public String profileToken;
    public String CompanyID;
    public String DisplayName;

    public String visitor_id;
    public String visitor_name;

    private EditText chat_message;

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

        SharedPreferences prefs = getSharedPreferences("zupportdesk", MODE_PRIVATE);
        ProfileId = prefs.getString("ProfileId", "Not defined");
        profileToken = prefs.getString("profileToken", "Not defined");
        CompanyID = prefs.getString("companyId", "Not defined");
        DisplayName = prefs.getString("DisplayName", "Not defined");

        chat_message = (EditText) findViewById(R.id.ET_chating_message);
        baseContext = getBaseContext();

        Intent intent = getIntent();
        visitor_id = intent.getStringExtra("visitor_id");
        visitor_name = intent.getStringExtra("visitor_name");

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        initNavigationDrawer();
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(visitor_name);

        uniqueID = UUID.randomUUID().toString();
        Log.d("GUIID", uniqueID);

        // add some items to list to test
        messageItems.add(new ChattingItomObject("VisitorID", visitor_name+" has joined the chat!", "", "Other", "UserName", "Other", uniqueID));
        messageItems.add(new ChattingItomObject("VisitorID", "Hi, how can I help you today?", "", "Other", "UserName", "Other", uniqueID));
        messageItems.add(new ChattingItomObject("VisitorID", DisplayName+" has joined the chat!", "", "Other", "UserName", "Other", uniqueID));

        recyclerView = (RecyclerView) findViewById(R.id.recycler_chat);
        mAdapter = new ChattingAdapter(this, messageItems);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);
    }


    public static void updateChatting(String visitor_id, String message, String date_time, String operator_type, String visitor_name){

        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(Chatting.baseContext, notification);
        r.play();

        uniqueID = UUID.randomUUID().toString();
        Log.d("GUIID", uniqueID);

        messageItems.add(new ChattingItomObject(visitor_id, message, date_time, operator_type, visitor_name, "Message_Type", uniqueID));

        Handler refresh = new Handler(Looper.getMainLooper());
        refresh.post(new Runnable() {
            public void run(){mAdapter.notifyDataSetChanged();}
        });

    }
活动聊天XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    tools:context="zupportdesk.desk.zupport.chatsystem.Chatting">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.learn2crack.myapplication.MainActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:id="@+id/user_details_app_bar"
            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>


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

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recycler_chat"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true"
                        android:scrollbars="none"
                        android:layout_marginTop="55dp"
                        android:layout_marginBottom="45dp" />

                            <RelativeLayout
                                android:layout_width="match_parent"
                                android:layout_height="50dp"
                                android:background="@color/colorPrimaryDark"
                                android:layout_alignParentBottom="true"
                                android:layout_alignParentLeft="true"
                                android:layout_alignParentStart="true">
                                <EditText
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="1"
                                    android:textColorHint="#CFD8DC"
                                    android:textColor="#CFD8DC"
                                    android:hint="Write a message"
                                    android:id="@+id/ET_chating_message"
                                    android:layout_alignParentBottom="true"
                                    android:layout_alignParentLeft="true"
                                    android:layout_alignParentStart="true"
                                    android:layout_toLeftOf="@+id/IV_Chatting_send"
                                    android:layout_toStartOf="@+id/IV_Chatting_send"
                                    android:layout_marginLeft="10dp" />

                                <ImageView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="5"
                                    android:padding="4dp"
                                    android:onClick="send_chat_message"
                                    android:src="@android:drawable/ic_menu_send"
                                    android:id="@+id/IV_Chatting_send"
                                    android:layout_alignBottom="@+id/ET_chating_message"
                                    android:layout_alignParentRight="true"
                                    android:layout_alignParentEnd="true" />
                            </RelativeLayout>

                </RelativeLayout>


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


    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_navigation"/>
</android.support.v4.widget.DrawerLayout>


当您向适配器添加新消息时,是否有人可以帮助我在更新时在
updateCatting
方法中将Recyclerview滚动到底。

。那个时候调用这个方法

recyclerViewObject.scrollToPosition(chatArrayList.size() - 1);

将新消息添加到适配器时。那个时候调用这个方法

recyclerViewObject.scrollToPosition(chatArrayList.size() - 1);

在notifyDataSetChanged()之后调用此函数。:

另外,当您仅向recyclerView添加新项目时,而不是调用
notifyDataSetChanged()
时,您可以调用:

mAdapter.notifyItemInserted(mAdapter.getItemCount()-1);

在notifyDataSetChanged()之后调用此函数。:

另外,当您仅向recyclerView添加新项目时,而不是调用
notifyDataSetChanged()
时,您可以调用:

mAdapter.notifyItemInserted(mAdapter.getItemCount()-1);

在更新回收视图时调用此函数

recyclerView.post(new Runnable() {
    @Override
    public void run() {

        recyclerView.smoothScrollToPosition(adapter.getItemCount());                                 
    }
});

在更新回收视图时调用此函数

recyclerView.post(new Runnable() {
    @Override
    public void run() {

        recyclerView.smoothScrollToPosition(adapter.getItemCount());                                 
    }
});
这是聊天应用程序的最佳选择

这是聊天应用程序的最佳选择


Tnx兄弟。你节省了我的时间。你节省了我的时间。
 recyclerView.setAdapter(adapter);
 recyclerView.getAdapter().getItemCount();
 adapter.notifyDataSetChanged();
 recyclerView.getLayoutManager().scrollToPosition(user_arr.size() - 1);