ListView doesn't scroll to end, when keyboard using

痞子三分冷 提交于 2019-12-12 08:44:01

问题


My ListView should automatically scroll to end, when new message received or sent. But when I use Android keyboard to send message from EditText below ListView, ListView resized and new message, which I want to send, appears out of screen and I have to scroll down to see it, even if keyboard disappears and ListView resizes again.

To scroll ListView I use:

listView.postDelayed(new Runnable() {
@Override public void run() { listView.setSelection(listView.getCount()); } }, 100);

but is not working correctly in my case.

Does anyone know why it may occur? Is there a way, which ALWAYS scroll ListView to end?

Thanks, your help is much appreciated!


回答1:


You can try to add this line of code after you set selection to your list:

listView.smoothScrollToPosition(listView.getCount());

Resulting code should look like this:

listView.postDelayed(new Runnable() {
        @Override
        public void run() {
            listView.setSelection(listView.getCount());
            listView.smoothScrollToPosition(listView.getCount());
        }
    }, 100);



回答2:


Try this insead of your code:

listView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

(or set it by XML layout).




回答3:


Set android:windowSoftInputMode in manifest for the activity to adjustPan|adjustResize.



来源:https://stackoverflow.com/questions/12087446/listview-doesnt-scroll-to-end-when-keyboard-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!