Android 当用户到达底部时,从JSON动态填充listview

Android 当用户到达底部时,从JSON动态填充listview,android,android-listview,android-json,Android,Android Listview,Android Json,我正在通过JSON填充一个ListView。我很快就完成了 我面临的唯一问题是:当用户到达列表视图的末尾时,我想加载更多的项目,但我不想在开头加载100个项目。我想显示10个项目,然后再显示10个项目。我如何才能做到这一点?遵循以下步骤:- 大多数代码都有注释,因此您可以了解每个语句的作用 将FooterView添加到ListView 页脚视图只不过是一段XML,它定义了listview的页脚的外观 <LinearLayout xmlns:android="http://schemas.a

我正在通过JSON填充一个
ListView
。我很快就完成了

我面临的唯一问题是:当用户到达
列表视图的末尾时,我想加载更多的项目,但我不想在开头加载100个项目。我想显示10个项目,然后再显示10个项目。我如何才能做到这一点?

遵循以下步骤:-
大多数代码都有注释,因此您可以了解每个语句的作用

将FooterView添加到ListView 页脚视图只不过是一段XML,它定义了listview的页脚的外观

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:gravity="center_horizontal"
  android:padding="3dp"
  android:layout_height="fill_parent">
  <TextView
    android:id="@id/android:empty"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:padding="5dp"
    android:text="Loading more days..."/>
</LinearLayout>
现在是有趣的部分,检查我们是否一路下来并装载物品

   private Runnable loadMoreListItems = new Runnable() {
    @Override
    public void run() {
        //Set flag so we cant load new items 2 at the same time
        loadingMore = true;
        //Reset the array that holds the new items
        myListItems = new ArrayList<String>();
        //Simulate a delay, delete this on a production environment!
        try { Thread.sleep(1000);
        } catch (InterruptedException e) {}
        //Get 15 new listitems
        for (int i = 0; i < itemsPerPage; i++) {
            //Fill the item with some bogus information
                myListItems.add("Date: " + (d.get(Calendar.MONTH)+ 1) + "/" + d.get(Calendar.DATE) + "/" + d.get(Calendar.YEAR) );
            // +1 day
                d.add(Calendar.DATE, 1);
        }
        //Done! now continue on the UI thread
            runOnUiThread(returnRes);
    }
};
Java实现 实际加载在单独的线程中完成(可运行)。这样我们就不会锁定主界面(GUI)

实现Runnable以处理加载 加载由2个
Runnables
完成。调用第一个runnable
loadMoreListItems
以获取数据,在主(UI)线程上调用第二个runnable
returnRes
以更新接口。要从第一个切换到第二个,我们使用一个名为:

以下是实现Runnables的代码,我尽可能多地对代码进行了注释以使其易于理解:

可运行以加载项目

   private Runnable loadMoreListItems = new Runnable() {
    @Override
    public void run() {
        //Set flag so we cant load new items 2 at the same time
        loadingMore = true;
        //Reset the array that holds the new items
        myListItems = new ArrayList<String>();
        //Simulate a delay, delete this on a production environment!
        try { Thread.sleep(1000);
        } catch (InterruptedException e) {}
        //Get 15 new listitems
        for (int i = 0; i < itemsPerPage; i++) {
            //Fill the item with some bogus information
                myListItems.add("Date: " + (d.get(Calendar.MONTH)+ 1) + "/" + d.get(Calendar.DATE) + "/" + d.get(Calendar.YEAR) );
            // +1 day
                d.add(Calendar.DATE, 1);
        }
        //Done! now continue on the UI thread
            runOnUiThread(returnRes);
    }
};
private Runnable loadMoreListItems=new Runnable(){
@凌驾
公开募捐{
//设置标志,以便我们不能同时加载新项目2
loadingMore=true;
//重置包含新项的数组
myListItems=新的ArrayList();
//模拟延迟,在生产环境中删除此延迟!
试试{Thread.sleep(1000);
}捕获(中断异常e){}
//获取15个新列表项
对于(int i=0;i
第二个是可运行的: 因为我们不能从线程更新UI,所以这个Runnable会解决这个问题

private Runnable returnRes = new Runnable() {
    @Override
    public void run() {
        //Loop thru the new items and add them to the adapter
        if(myListItems != null && myListItems.size() > 0){
                    for(int i=0;i < myListItems.size();i++)
                    adapter.add(myListItems.get(i));
                 }
        //Update the Application title
            setTitle("Neverending List with " + String.valueOf(adapter.getCount()) + " items");
        //Tell to the adapter that changes have been made, this will cause the list to refresh
                adapter.notifyDataSetChanged();
        //Done loading more.
                loadingMore = false;
        }
   };
private Runnable returnRes=new Runnable(){
@凌驾
公开募捐{
//循环浏览新项目并将其添加到适配器
if(myListItems!=null&&myListItems.size()>0){
对于(int i=0;i
该项目正是为此而设计的。它说它现在已经退役了,没有列出任何解释或替代品,但它工作得非常好

private Runnable returnRes = new Runnable() {
    @Override
    public void run() {
        //Loop thru the new items and add them to the adapter
        if(myListItems != null && myListItems.size() > 0){
                    for(int i=0;i < myListItems.size();i++)
                    adapter.add(myListItems.get(i));
                 }
        //Update the Application title
            setTitle("Neverending List with " + String.valueOf(adapter.getCount()) + " items");
        //Tell to the adapter that changes have been made, this will cause the list to refresh
                adapter.notifyDataSetChanged();
        //Done loading more.
                loadingMore = false;
        }
   };