简体   繁体   中英

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. When I click on a member I get another screen with a list (RecyclerView) of all the messages of that selected member.

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. 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. This method should be called in activity onPause method when the activity is paused so as to save them as:

    @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.

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

   @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.

Note If its the first time the user has visited the chat the default position is 0 and the default string is "" empty String. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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