简单的分页加载实现

------------------------------main.activity--------------------------------

package com.example.listview_scroll;


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.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;
/              
public class MainActivity extends Activity {

    private ListView listview;
    private ArrayList<String> list;
    private DataService dataService;
    private View footer;
    private final int SUCCESS=0;//加载成功标志
    private boolean finish=true;//是否要加载
    
    //Handler
    private Handler handler=new Handler(){
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0:
                ArrayList<String> result=(ArrayList<String>) msg.obj;//获得传来的集合
                list.addAll(result);//把集合添加进去
                adapter.notifyDataSetChanged();//刷新listview
                finish=true;//加载完成
                if (finish) {
                    //删除页脚
                    if(listview.getFooterViewsCount()>0){
                        listview.removeFooterView(footer);
                    }
                }
                break;
            }
        };
    };

    private ArrayAdapter<String> adapter;    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //找控件
        listview=(ListView)findViewById(R.id.listview);
        list=new ArrayList<String>();
        //给集合添加数据
        dataService = new DataService();
        List<String> data = dataService.getData();
        list.addAll(data);
        adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item, R.id.tv_info, list);
        //添加页脚
        footer = View.inflate(getApplicationContext(), R.layout.footer, null);
        listview.addFooterView(footer);
        listview.setAdapter(adapter);
        //移出页脚
        listview.removeFooterView(footer);
        //设置滚动监听
        listview.setOnScrollListener(new MyListener());
    }
    private class MyListener implements OnScrollListener{
        
        private int countPage=5;//加载总页数
        private int pageSize =20;//每次加载的数据个数
        
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            final int currentCount=firstVisibleItem+visibleItemCount;//已经加载的条数
            int currentPage=currentCount/pageSize;//当前页数
            int nextPage=currentPage+1;//下一页
            Log.i("--------------->>>", "执行"+currentCount+"!"+totalItemCount+"!"+nextPage+"!"+currentPage);
            if(currentCount==totalItemCount&&nextPage<=countPage&&finish){
                Log.i("--------------->>>", "执行2");
                finish=false;//不需要加载
                listview.addFooterView(footer);//加载中设置页脚
                new Thread(){
                    public void run() {
                        SystemClock.sleep(3000);//睡眠3秒,不会立即加载
                        List<String> result=dataService.getData();
                        //发送消息
                        Message  message=Message.obtain();
                        message.what=SUCCESS;
                        message.obj=result;
                        handler.sendMessage(message);
                    };
                }.start();
            }
        }
    }
    //获得数据类
    public class DataService{
//        public List<String> getData(int start,int count){//里面可以指定值
        public List<String> getData(){
            List<String> data=new ArrayList<String>();
            for (int i = 1; i < 21; i++) {
                data.add("数据"+i);
            }
            return data;
        }
    }

}


------------------------------main.xml--------------------------------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

   <ListView
       android:id="@+id/listview"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
   </ListView>

</RelativeLayout>

------------------------------footer.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="horizontal" >
    
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

------------------------------item.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" >
    
    <TextView
        android:id="@+id/tv_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="item"
        android:textColor="#000000"/>

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值