android ListView分页加载

android ListView分页加载

本文主要参考文章:Android数据分批加载-滑动到底部自动加载列表
网页里面一般就是有分页控件,然后分别给出pagesize和nowpagenumber就ok了,但是移动端的手机上不能这样做,分页就是通过一个简答的滑动实现的,例如:微信的朋友圈之类的。好了,下面上货==>
1、布局文件:



    

    


2、需要的一些资源(list列表样式和footer底部样式):
android ListView分页加载_第1张图片
footer.xml



    
    

simple_list.xml


    

3、MainActivity.java
package com.example.administrator.datapageload;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private final String TAG = MainActivity.class.getSimpleName();
    private ListView listView;
    private View footer;

    private List data = new ArrayList();
    ArrayAdapter adapter;
    private boolean loadFinishFlag;
    private int startIndex;
    private int endIndex;
    private final int pageSize = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) this.findViewById(R.id.listview);
        footer = getLayoutInflater().inflate(R.layout.footer, null);


        loadFinishFlag = true;
        startIndex = 0;
        endIndex = pageSize;
        data.addAll(getDataService(startIndex, endIndex));
        adapter = new ArrayAdapter(this, R.layout.simple_list, R.id.simple_list_text, data);
        listView.setAdapter(adapter);
        listView.addFooterView(footer);
        listView.setOnScrollListener(new ScrollListener());
        listView.removeFooterView(footer);
    }

    /**
     * 模拟加载数据
     *
     * @param from
     * @param to
     * @return
     */
    public List getDataService(int from, int to) {
        List resList = new ArrayList<>();
        for (int i = from; i < to; i++) {
            resList.add("测试数据" + i);
        }
        return resList;
    }

    public final class ScrollListener implements AbsListView.OnScrollListener {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            Log.i(TAG, "---->" + scrollState);
            switch (scrollState) {
                case SCROLL_STATE_IDLE:
                    break;
                case SCROLL_STATE_TOUCH_SCROLL:
                    break;
                case SCROLL_STATE_FLING:
                    break;
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            //获取屏幕最后Item的ID
            int lastVisibleItem = listView.getLastVisiblePosition();
            if (lastVisibleItem + 1 == totalItemCount) {
                if (loadFinishFlag) {
                    //标志位,防止多次加载
                    loadFinishFlag = false;
                    listView.addFooterView(footer);
                    //开线程加载数据
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            startIndex += pageSize;
                            endIndex += pageSize;
                            Message message = handler.obtainMessage(0x123, getDataService(startIndex, endIndex));
                            message.sendToTarget();
                        }
                    }.start();
                }
            }
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x123) {
                data.addAll((List) msg.obj);
                adapter.notifyDataSetChanged();
                listView.removeFooterView(footer);
                loadFinishFlag = true;
            }
        }
    };
}

4、总结一下,就是监听滑动时间,发现滑动到底端的时候,进行继续加载。当然,加载采用线程和Handler的方式。需要注意的是加入标志位,如果不加,会加载很多次。

下面是运行结果:
android ListView分页加载_第2张图片

在加载20条:
android ListView分页加载_第3张图片
再加载20条:
android ListView分页加载_第4张图片
最后附上项目代码:点我下载,不要你的积分哦!!!

你可能感兴趣的:(Android)