天威茫然

既然不是天才,那就努力成为一个人才吧!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

参考资料:http://www.cnblogs.com/noTice520/archive/2012/02/10/2345057.html#2458464

效果图:

MAINUI:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview_bags"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fastScrollEnabled="true"
        android:focusable="true"
        android:cacheColorHint="#ffffffff" />

</LinearLayout>

 

LoadUi:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bt_load"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="加载更多数据" />

    <ProgressBar
        android:id="@+id/pg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:visibility="gone" />

</LinearLayout>

domain:

View Code
<?xml version="1.0" encoding="UTF-8"?>
<persons>
    <person id="1">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="2">
        <name>lixiangmei</name>
        <age>21</age>
    </person>
    <person id="3">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="4">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="5">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="6">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="7">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="8">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="9">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="10">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="11">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="12">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="13">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="14">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="15">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="16">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="17">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="18">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="19">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="20">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="21">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="22">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="23">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="1">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="2">
        <name>lixiangmei</name>
        <age>21</age>
    </person>
    <person id="3">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="4">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="5">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="6">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="7">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="8">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="9">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="10">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="11">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="12">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="13">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="14">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="15">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="16">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="17">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="18">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="19">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="20">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="21">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="22">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="23">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="1">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="2">
        <name>lixiangmei</name>
        <age>21</age>
    </person>
    <person id="3">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="4">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="5">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="6">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="7">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="8">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="9">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="10">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="11">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="12">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="13">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="14">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="15">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="16">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="17">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="18">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="19">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="20">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="21">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="22">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="23">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="1">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="2">
        <name>lixiangmei</name>
        <age>21</age>
    </person>
    <person id="3">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="4">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="5">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="6">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="7">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="8">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="9">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="10">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="11">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="12">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="13">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="14">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="15">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="16">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="17">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="18">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="19">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="20">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="21">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="22">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="23">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="1">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="2">
        <name>lixiangmei</name>
        <age>21</age>
    </person>
    <person id="3">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="4">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="5">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="6">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="7">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="8">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="9">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="10">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="11">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="12">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="13">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="14">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="15">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="16">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="17">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="18">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="19">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="20">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="21">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="22">
        <name>huanren</name>
        <age>22</age>
    </person>
    <person id="23">
        <name>huanren</name>
        <age>22</age>
    </person>
</persons>

 

java:

/**
 * 
 */
package com.diamond.util.domain;

/**
 * 作用:领域实体类Person
 * 创建时间:2012年7月12日15:16:59
 * 修改时间:2012年7月12日15:17:03
 * @author Hzl520
 */
public class Person {
    private Integer id;
    private String name;
    private Short age;
    /**
     * @param id
     * @param name
     * @param age
     */
    public Person(Integer id, String name, Short age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Person() {}
    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the age
     */
    public Short getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(Short age) {
        this.age = age;
    }
    /* 
     * 以字符串显示
     */
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
}

 

解析XML:

/**
 * 
 */
package com.diamond.util.services;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

import com.diamond.util.domain.Person;



/**
 * 作用:Pull解析XML
 * 创建时间:2012-7-14 上午10:29:29
 * 修改时间:2012-7-14 上午10:29:29
 * @author Hzl520
 */
public class PullPersonPaseService {
    /**
     * 作用:以Pull方式获取XML信息
     * 步骤:看注释
     * 创建时间:2012-7-14 上午10:31:25
     * 修改时间:2012-7-14 上午10:31:25
     * @param inputStream
     * @return
     * @throws Exception 
     */
    public  List<Person> getPersons(InputStream inputStream) throws Exception {
        List<Person> persons=null;
        Person person=null;
        XmlPullParser parser=Xml.newPullParser();
        parser.setInput(inputStream, "UTF-8");
        //获取解析事件,和SAX类似
        int eventType=parser.getEventType();
        //使用各个事件解析XML
        while(eventType!=XmlPullParser.END_DOCUMENT){
            switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                //开始遍历节点的时候实例化集合
                persons=new ArrayList<Person>();
                break;
            case XmlPullParser.START_TAG:
                String name=parser.getName();
                if(name.equals("person")){
                    person=new Person();
                    person.setId(new Integer(parser.getAttributeValue(0)));
                }
                if (person!=null) {
                    if (name.equals("name")) {
                        person.setName(parser.nextText());
                    }
                    if (name.equals("age")) {
                        person.setAge(new Short(parser.nextText()));
                    }
                }
                break;
            case XmlPullParser.END_TAG:
                if(parser.getName().equals("person")){
                    persons.add(person);
                    person=null;
                    }
                break;
            }
            //遍历下一个节点
            eventType=parser.next();
        }
        return persons;
    }
}

 

main:

/**
 * 
 */
package com.diamond.util.widget;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.lang.reflect.Field;
import com.diamond.util.R;
import com.diamond.util.domain.Person;
import com.diamond.util.services.PullPersonPaseService;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.graphics.drawable.Drawable;

/**
 * 作用: 创建时间:2012-8-28 下午7:32:23 修改时间:2012-8-28 下午7:32:23
 * 
 * @author Hzl520
 */
public class ListViewFoxmlLoadActivity extends Activity implements
        OnScrollListener {

    // ListView的Adapter
    private SimpleAdapter mSimpleAdapter;
    private ListView lv;
    private Button bt;
    private ProgressBar pg;
    private ArrayList<HashMap<String, String>> list;
    // ListView底部View
    private View moreView;
    private Handler handler;
    // 设置一个最大的数据条数,超过即不再加载
    private int MaxDateNum;
    // 最后可见条目的索引
    private int lastVisibleIndex;

    PullPersonPaseService pullPersonPaseService = null;

    /*
     * 作用:初始化加载中... 步骤:看注释 创建时间:2012-8-28 下午7:33:06 修改时间:2012-8-28 下午7:33:06
     * 
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewfoxmlload);
        lv = (ListView) findViewById(R.id.listview_bags);
        // 实例化底部布局
        moreView = getLayoutInflater().inflate(
                R.layout.listviewfoxmlloadmoredata, null);
        bt = (Button) moreView.findViewById(R.id.bt_load);
        pg = (ProgressBar) moreView.findViewById(R.id.pg);
        handler = new Handler();

        // 数据处理
        list = new ArrayList<HashMap<String, String>>();
        pullPersonPaseService = new PullPersonPaseService();
        // 使用类加载器获取本地XML的输入流
        InputStream inputStream = getClass().getClassLoader()
                .getResourceAsStream("com/diamond/util/access/person.xml");
        try {
            List<Person> persons = pullPersonPaseService
                    .getPersons(inputStream);
            MaxDateNum = persons.size();// 设置最大数据条数
            // 用map来装载数据,初始化10条数据
            for (Person person : persons) {
                if (persons.indexOf(person) < 10) {// 获取XML中Top10的内容
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("ItemTitle", person.getId().toString());
                    map.put("ItemText", person.getName().toString());
                    list.add(map);
                } else {
                    continue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 实例化SimpleAdapter
        mSimpleAdapter = new SimpleAdapter(this, list,
                R.layout.listviewfoxmlloadmapitem, new String[] { "ItemTitle",
                        "ItemText" }, new int[] { R.id.tv_title,
                        R.id.tv_content });
        // 加上底部View,注意要放在setAdapter方法前
        lv.addFooterView(moreView);
        lv.setAdapter(mSimpleAdapter);
        // 绑定监听器

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                ListView listView = (ListView) parent;
                HashMap<String, Object> itemmap = (HashMap<String, Object>) listView
                        .getItemAtPosition(position);
                String title = itemmap.get("ItemTitle").toString();
                Toast.makeText(getApplicationContext(), title, 1).show();
            }
        });

        lv.setOnScrollListener(this);

        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                pg.setVisibility(View.VISIBLE);// 将进度条可见
                bt.setVisibility(View.GONE);// 按钮不可见

                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        loadMoreDate();// 加载更多数据
                        bt.setVisibility(View.VISIBLE);
                        pg.setVisibility(View.GONE);
                        mSimpleAdapter.notifyDataSetChanged();// 通知listView刷新数据
                    }

                }, 2000);
            }
        });

    }

    /**
     * 作用: 步骤:看注释 创建时间:2012-8-28 下午8:31:52 修改时间:2012-8-28 下午8:31:52
     */
    private void loadMoreDate() {
        int count = mSimpleAdapter.getCount();// 列表加载的当前数据的长度
        pullPersonPaseService = new PullPersonPaseService();
        // 使用类加载器获取本地XML的输入流
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("com/diamond/util/access/person.xml");
        try {
            List<Person> persons = pullPersonPaseService
                    .getPersons(inputStream);
            if (count + 5 < MaxDateNum) {
                // 每次加载5条
                for (Person person : persons) {
                    if (persons.indexOf(person) >= count
                            && persons.indexOf(person) < count + 5) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("ItemTitle", person.getId().toString());
                        map.put("ItemText", person.getName().toString());
                        list.add(map);
                    } else {
                        continue;
                    }
                }

            } else {
                // 数据已经不足5条
                for (Person person : persons) {
                    if (persons.indexOf(person) >= count
                            && persons.indexOf(person) < MaxDateNum) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("ItemTitle", person.getId().toString());
                        map.put("ItemText", person.getName().toString());
                        list.add(map);
                    } else {
                        continue;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*
     * 作用: 步骤:看注释 创建时间:2012-8-28 下午7:32:23 修改时间:2012-8-28 下午7:32:23
     * 
     * @param view
     * 
     * @param scrollState
     */
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // 滑到底部后自动加载,判断listview已经停止滚动并且最后可视的条目等于adapter的条目
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
                && lastVisibleIndex == mSimpleAdapter.getCount()) {
            // 当滑到底部时自动加载
            // pg.setVisibility(View.VISIBLE);
            // bt.setVisibility(View.GONE);
            // handler.postDelayed(new Runnable() {
            //
            // @Override
            // public void run() {
            // loadMoreDate();
            // bt.setVisibility(View.VISIBLE);
            // pg.setVisibility(View.GONE);
            // mSimpleAdapter.notifyDataSetChanged();
            // }
            //
            // }, 2000);

        }

    }

    /*
     * 作用: 步骤:看注释 创建时间:2012-8-28 下午7:32:23 修改时间:2012-8-28 下午7:32:23
     * 
     * @param view
     * 
     * @param firstVisibleItem
     * 
     * @param visibleItemCount
     * 
     * @param totalItemCount
     */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        // 计算最后可见条目的索引
        lastVisibleIndex = firstVisibleItem + visibleItemCount - 1;

        // 所有的条目已经和最大条数相等,则移除底部的View
        if (totalItemCount == MaxDateNum + 1) {
            bt.setText("数据全部加载完成,没有更多数据!");
        }
    }

}

 

 

 

 

posted on 2012-08-30 09:28  天威茫然  阅读(1514)  评论(2编辑  收藏  举报