Android_自己写的简单上拉加载

来源:互联网 发布:淘宝买的鲜人参怎么样 编辑:程序博客网 时间:2024/06/03 15:02

DO:
main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/listView_listView1"        android:layout_width="match_parent"        android:layout_height="match_parent"        /></LinearLayout>

footer.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ProgressBar        android:id="@+id/progressBar1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@+id/progressBar1"        android:layout_marginTop="10dp"        android:text="加载中......"        android:textAppearance="?android:attr/textAppearanceLarge" /></RelativeLayout>

Activity

package com.lxf;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.SystemClock;import android.view.View;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;/** * @address BeiJing * @author LiXufei * @function android_listview */public class cListView extends Activity{    private ListView view1;    private List<String> list = new ArrayList<String>();    private ArrayAdapter<String> adapter;    private final int SUCCESS = 0;      //加载成功标志    private boolean flag = true;    //是否刷新    private View footerView;    //页脚    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.android_listview);        view1 = (ListView) findViewById(R.id.listView_listView1);        //初始化页脚        footerView = getLayoutInflater().inflate(R.layout.android_listview_footer,null );        //初始化数据        list.addAll(addData(20));        //初始化适配器        adapter = new ArrayAdapter<String>(cListView.this, android.R.layout.simple_list_item_1, list);        //适配器        view1.setAdapter(adapter);        //设置滚动监听        view1.setOnScrollListener(new MyScrollListner(5, 20));    }    //handler    private Handler handler = new Handler(){        public void handleMessage(android.os.Message msg) {            if (msg.what == SUCCESS) {                flag = true;                List<String>res = (List<String>) msg.obj ;                list.addAll(res);                adapter.notifyDataSetChanged();                Toast.makeText(cListView.this, "加载成功!", 0).show();            }            if (flag) {                if (view1.getFooterViewsCount() > 0) {                    view1.removeFooterView(footerView);                }            }        };    };    //滚动刷新类    class MyScrollListner implements OnScrollListener{        private int pageCount;  //加载的页数        private int pageSize;   //加载的个数        public MyScrollListner(int countPage, int sizePage) {            super();            this.pageCount = countPage;            this.pageSize = sizePage;        }        public void onScrollStateChanged(AbsListView view, int scrollState) {       }        public void onScroll(AbsListView view, int firstVisibleItem,                int visibleItemCount, int totalItemCount) {            int currentCount = firstVisibleItem+visibleItemCount;   //当前个数            int currentPage = currentCount/pageSize;    //当前的页数            int nextPage = currentPage+1;   //下个页数            if (currentCount == totalItemCount && nextPage <= pageCount && flag) {  //执行数据刷新                view1.addFooterView(footerView);    //添加页脚                flag = false;   //改变标志                new Thread(){                    public void run() {                        SystemClock.sleep(3000);//睡眠3秒,不会立即加载                        List<String> addData = addData(pageSize);   //获取新数据                        //通过handler更新listview                        Message message = Message.obtain();                        message.what = SUCCESS;                        message.obj = addData;                        handler.sendMessage(message);                    };                }.start();            }        }    }    //添加数据方法    public List<String> addData(int Count){        ArrayList<String> datas = new ArrayList<String>();        //准备数据        for (int i = 0; i <= Count; i++) {            datas.add("数据"+i);        }        return datas;    }}
1 0
原创粉丝点击