[英]android ListView : scrollTo doesn't work
我有一個視圖,其中包含一個綁定到游標適配器的ListView。 當光標內容發生變化時,我想將ListView保持在頂部,然后在我的自定義光標適配器中添加:
@Override
protected void onContentChanged() {
// ...
myListView.scrollTo(0, 0);
}
但這不起作用。 然后我讀到某個地方將這個動作排隊如下:
myListView.post(new Runnable() {
public void run() {
myListView.scrollTo(0, 0);
}
});
但這也不起作用。
當內容發生變化時,如何將ListView保持在頂部?
編輯:
只是為了嘗試,我添加了一個按鈕,並在其onClickListener中調用scrollTo(),它不起作用! 我錯過了什么?
而不是scrollTo ,嘗試使用setSelection(0)到達列表視圖的頂部位置。
我制作的功能對於listview滾動的其他人很有用,它們適用於我的每個Android版本,模擬器和設備,這里itemheight是listview中固定的視圖高度。
int itemheight=60;
public void scrollToY(int position)
{
int item=(int)Math.floor(position/itemheight);
int scroll=(int) ((item*itemheight)-position);
this.setSelectionFromTop(item, scroll);// Important
}
public void scrollByY(int position)
{
position+=getListScrollY();
int item=(int)Math.floor(position/itemheight);
int scroll=(int) ((item*itemheight)-position);
this.setSelectionFromTop(item, scroll);// Important
}
public int getListScrollY()
{
try{
//int tempscroll=this.getFirstVisiblePosition()*itemheight;// Important
View v=this.getChildAt(0);
int tempscroll=(this.getFirstVisiblePosition()*itemheight)-v.getTop();// Important
return tempscroll;
}catch(Exception e){}
return 0;
}
ListView的scrollTo
作為View
自身適用於ListView
setSelection(0)
可以解決問題,因為它適用於listview的適配器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.