保持ListView控件可见底元素键盘出现时 [英] keeping bottom element of ListView visible when keyboard appears
问题描述
我有一个的LinearLayout
同时包含的ListView
和的EditText
。当屏幕上的键盘通过触摸推出的的EditText
的的ListView
调整大小,使只有前几名的元素保持可见的。
I have a LinearLayout
containing both a ListView
and an EditText
. When the On-Screen keyboard is launched by touching the EditText
, the ListView
resizes so that only the top few elements remain visible.
这将被用于的ListView
的上下文有底部的几个要素是更直观比顶部相关的,所以我想为它调整使底部保持可见,而不是顶部。任何指针?
The context that the ListView
is being used in has the bottom few elements being more visually relevant than the top, and so I'd like for it to resize so that the bottom remains visible, rather than the top. Any pointers?
(顺便说一句,我使用的是目前的修复涉及到使用 smoothScrollToPosition
,但laggy滚动行为使得这个令人不快的)
(Incidentally, the current fix I'm using involves using smoothScrollToPosition
, but the laggy scroll behaviour makes this undesirable)
推荐答案
我今天早上刚解决了类似的问题,我想我会在这里发表我的结果对未来搜索的好处。我的问题是,滚动至底部并没有帮助,因为我认为实际上改变大小之前调用它。解决方案?等到它通过使用GlobalLayoutListener改变大小
I just solved a similar issue this morning, and thought I'd post my result here for the benefit of future searchers. My issue was that scrolling to the bottom wasn't helping since I was calling it before the view actually changed size. The solution? Wait until it does change size by using a GlobalLayoutListener
步骤:
1)实施活动如下方法持有的ListView
Steps: 1) implement the following method in the activity holding the listview
public void scrollToBottom(){
//I think this is supposed to run on the UI thread
listView.setSelection(mAdapter.getCount() - 1);
}
2)创建下面的类
2) create the following class
public class OnGlobalLayoutListenerWithScrollToBottom implements OnGlobalLayoutListener{
private boolean scroll;
private OnScrollToBottomListener listener;
public interface OnScrollToBottomListener{
public void scrollToBottom();
}
public OnGlobalLayoutListenerWithScrollToBottom(OnScrollToBottomListener listener){
this.listener = listener;
}
@Override
public void onGlobalLayout() {
if(scroll){
listener.scrollToBottom();
scroll = false;
}
}
/**
* Lets the listener know to request a scroll to bottom the next time it is layed out
*/
public void scrollToBottomAtNextOpportunity(){
scroll = true;
}
};
3)在你的活动,实施从这个类的接口。然后,在你的活动,创建这个OnGlobalLayoutListener的一个实例,并将其设置为监听器为您的ListView
3) In your activity, implement the interface from this class. Then, in your activity, create an instance of this OnGlobalLayoutListener and set it as the listener for your listView
//make sure your class implements OnGlobalLayoutListenerWithScrollToBottom.OnScrollToBottomListener
listViewLayoutListener = new OnGlobalLayoutListenerWithScrollToBottom(this);
listView.getViewTreeObserver().addOnGlobalLayoutListener(listViewLayoutListener);
4)在你的活动,你做出改变,这将影响到列表视图的大小,如显示和隐藏其他视图或添加的东西到ListView之前,干脆让布局听众知道下次有机会来滚动
4) In your activity, before you make changes that will affect the size of list view, such as showing and hiding other views or adding stuff to the listview, simply let the layout listener know to scroll at the next opportunity
listViewLayoutListener.scrollToBottomAtNextOpportunity();
这篇关于保持ListView控件可见底元素键盘出现时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!