简体   繁体   English

Android在聊天应用中保持滚动位置和文本

[英]Android keep scroll position and text in chat app

I'm developing an Android chat App and I have a screen with a list (RecyclerView) of all chat members to chat with. 我正在开发一个Android聊天应用程序,并且有一个屏幕,其中包含要聊天的所有聊天成员的列表(RecyclerView)。 When I click on a member I get another screen with a list (RecyclerView) of all the messages of that selected member. 当我单击一个成员时,会出现另一个屏幕,其中包含该选定成员的所有消息的列表(RecyclerView)。

How can I achieve to keep the scroll position of the list with message and save the composed text (in case the message wasn't send yet) when I switch to another member? 当我切换到另一个成员时,如何实现保持消息滚动列表的位置并保存撰写的文本(以防消息尚未发送)? I want to restore those settings when returning to the specific list. 返回特定列表时,我想恢复这些设置。

Here is what you have to do. 这是您要做的。 First I have made an assumption that you have an One Activity that monitor all the chats in the app and display them. 首先,我假设您拥有一个活动,该活动可以监视应用程序中的所有聊天并显示它们。 And as you have said each chat has an ID thats different from another chat. 如您所说,每个聊天的ID与另一个聊天的ID不同。 Please comment if the assumptions dont go with the nature of your app, so I can edit the answer! 如果这些假设与您的应用程序的性质不符,请发表评论,以便我编辑答案!

The solution I am giving is fast and will give you a position(recycler View) and chat text (in edit Text) even if the Phone was turned OFF you can still recover the values. 我提供的解决方案速度很快,即使您关闭了电话,您仍可以找回位置(“回收者视图”)和聊天文本(在编辑文本中),您仍然可以恢复这些值。

There are three methods to do this define them within your activity class: The first is: 有三种方法可以在活动类中定义它们:第一种是:

private boolean setChatScrollPositionAndText(String chat_text,int position,String chat_id){
    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt(chat_id+"position",position);
    editor.putString(chat_id+"text",chat_text);
    return editor.commit();
}

The second and third are: 第二和第三是:

private String getLastText(String chat_id){
    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    return sharedPref.getString(chat_id+"text","");
}
private int getLastPosition(String chat_id){
    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    return sharedPref.getInt(chat_id+"position",0);
}

The first method will accept three things chat_text , position , chat_id all this values can be obtained from your layout Views. 第一个方法将接受三件事chat_textpositionchat_id所有这些值都可以从布局视图中获取。 This method should be called in activity onPause method when the activity is paused so as to save them as: 暂停活动时,应在活动onPause方法中调用此方法,以将其保存为:

    @Override
protected void onPause() {
    super.onPause();
    setChatScrollPositionAndText("your chat text from edit text",0,"your unique chat id");
}

You can get those values to pass from chat text is from editText where the use types and the position from the adapter OR layout manager of the recyclerView and also pass the id of chat as the third parameter save them there as yes they are all saved. 您可以从editText获得这些值以从聊天文本中传递,其中editText的使用类型和position来自recyclerView的适配器或布局管理器,还可以传递chat的id作为第三个参数将其保存在那里,因为它们被保存了。

Then to retrieve all those values you have to call them in onResume method of your activity its good there even when your activity is just resumed 然后,要检索所有这些值,就必须在活动的onResume方法中对其进行调用,即使刚刚恢复活动也是如此

   @Override
protected void onResume() {
    super.onResume();
    String myTextWas=getLastText("your unique chat id");
    int my_recycler_position=getLastPosition("my unique chat id");
}

To retrieve you will use the second and third method, take this values and use them to set the text in the edit text and/or scroll the recyclerView to that position obtained. 要进行检索,您将使用第二种方法和第三种方法,采用此值并将其用于在编辑文本中设置文本和/或将recyclerView滚动到所获得的位置。

Note If its the first time the user has visited the chat the default position is 0 and the default string is "" empty String. 注意:如果这是用户第一次访问聊天,则默认位置为0 ,默认字符串为""空字符串。 This whole solution uses SharedPreferences which are ways to save key-value pairs in android You can train yourself and get more explanation from the official android training . 整个解决方案使用SharedPreferences,这是将键值对保存在android中的方法。您可以进行自我训练并从android官方培训中获得更多解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM