I’m implementing a simple chatting application which has a TextView for text messages and a EditText for input.
I update my TextView by following method:
private void addChatContent(String authorName, String content){
final String newLine = authorName+" : "+content+"\n";
chat_content.append(newLine);
scroll.post(new Runnable(){
@Override
public void run() {
scroll.smoothScrollTo(0, chat_content.getBottom()+5000);
}
});
}
A problem I’m facing is: when there are new incoming messages, UI thread will be busy for refreshing the TextView. It makes my EditText become lag, I can hardly edit my input.
I can not refresh the TextView with another thread, right?
So what should I do to overcome this limitation?
Could somebody give me some light? Thanks in advance.
Ultimately, and unfortunately, there is only one thread dedicated to the UI. If you are updating the TextView there is no way you can simultaneously have a lag-free experience with the EditText. You already know this, but I fear some people answering this question may not, so here’s a reference:
Hence, the answer is quite clear: don’t do this. Why does the Textview have to be absolutely, 100% up to date, as the user is updating the EditText field? Why are you scrolling all the way to the bottom; maybe you can get away with deleting most of the contents of the TextView and, when the user scrolls, dynamically re-adding content to it?
IMO you should focus on reducing the amount of work you need to do on the TextView.