By

Josef

当ListView到达底部时,请求更多的数据。


One solution is to implement an OnScrollListener and make changes (like adding items, etc.) to the ListAdapter at a convenient state in its onScroll method.

The following ListActivity shows a list of integers, starting with 40, adding items when the user scrolls to the end of the list.

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;     }   } } 

You should obviously use separate threads for long running actions (like loading web-data) and might want to indicate progress in the last list item (like the market or gmail apps do).


更多相关文章

  1. Android实现读写JSON数据的方法
  2. android 检测sqlite数据表和表中字段(列)是否存在 (转)
  3. android sqlite 自增数据类型
  4. android打开数据连接(目测最稳定方式)
  5. Android SQLite数据库存储实现
  6. Android与C++ 使用socket传输数据
  7. android 使用Http的POST方式读取网络数据
  8. Android 数据查询query函数参数解析

随机推荐

  1. Android对Linux内核的增强:Low Memory Kil
  2. 最新的数据显示,十分之四的 Android(安卓)
  3. 用于 Android(安卓)智能手机的 Android(
  4. Android应用程序线程消息循环模型分析(1)
  5. Android软件工程师之不归路
  6. Android中布局的巧妙设计【android进化二
  7. android recovery
  8. 想抢先体验Android操作系统的魅力吗?那就
  9. Android牛博
  10. 针对Android的Crash监控, 崩溃分析---推