1
我需要創建基於滾動位置動態加載數據的列表視圖。當用戶滾動到結尾時,就像在Facebook中一樣,它動態地添加更多行。有沒有標準的解決方案呢?Android:基於滾動位置動態加載數據的列表視圖
我需要創建基於滾動位置動態加載數據的列表視圖。當用戶滾動到結尾時,就像在Facebook中一樣,它動態地添加更多行。有沒有標準的解決方案呢?Android:基於滾動位置動態加載數據的列表視圖
嘗試這種::
public class Test extends ListActivity implements OnScrollListener {
Aleph0 adapter = new Aleph0();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(adapter);
getListView().setOnScrollListener(this);
}
public void onScroll(AbsListView view,
int firstVisible, int visibleCount, int totalCount) {
boolean loadMore = /* maybe add a padding */
firstVisible + visibleCount >= totalCount;
if(loadMore) {
adapter.count += visibleCount; // or any other amount
adapter.notifyDataSetChanged();
}
}
public void onScrollStateChanged(AbsListView v, int s) { }
class Aleph0 extends BaseAdapter {
int count = 40; /* starting amount */
public int getCount() { return count; }
public Object getItem(int pos) { return pos; }
public long getItemId(int pos) { return pos; }
public View getView(int pos, View v, ViewGroup p) {
TextView view = new TextView(Test.this);
view.setText("entry " + pos);
return view;
}
}
}
這是listView的默認行爲。只有在項目可見時纔會加載項目中的視圖。
更多:: HTTP://stackoverflow.com/questions/1080811/android-endless-list – 2011-12-21 08:06:40
感謝Dr.nik!我在這裏發現了一篇文章http://codinglines.frankiv.me/post/14552677846/android-implementing-a-dynamically-loading-adapter – yfranz 2011-12-21 22:40:24