ListView动态加载&从服务器检索数据

时间:2016-03-02 04:29:24

标签: android listview scrollview

我有listview项目超过500个产品。我需要检索数据限制50。每当我滚动时,列表必须加载到下50个项目。

您的回答更受赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:5)

试试这段代码: -

public class EndlessListView extends ListActivity implements AbsListView.OnScrollListener {

EndlessAdapter adapter = new EndlessAdapter();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(adapter);
    getListView().setOnScrollListener(this);
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;

    if (loadMore) {
        adapter.count += visibleItemCount; // or any other amount
        adapter.notifyDataSetChanged();
    }
}


class EndlessAdapter extends BaseAdapter {
    int count = 50; /* 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(EndlessListView.this);
        view.setText(String.format("entry %d", pos));
        return view;
    }
}
}