android ListView分页加载

android ListView分页加载

网页里面一般就是有分页控件,然后分别给出pagesize和nowpagenumber就ok了,但是移动端的手机上不能这样做,分页就是通过一个简答的滑动实现的,例如:微信的朋友圈之类的。好了,下面上货==>
1、布局文件:
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.administrator.datapageload.MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="列表分页加载"
        android:textColor="#f00"
        android:textSize="50sp" />

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

2、需要的一些资源(list列表样式和footer底部样式):

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:gravity="center"
    android:orientation="horizontal">

    <ProgressBar
        style="@android:style/Widget.ProgressBar.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="加载中..."/>
</LinearLayout>

simple_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/simple_list_text"
        android:textColor="#0f0"
        android:textSize="30sp"/>
</LinearLayout>

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<String> data = new ArrayList<String>();
    ArrayAdapter<String> 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<String>(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<String> getDataService(int from, int to) {
        List<String> 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<String>) msg.obj);
                adapter.notifyDataSetChanged();
                listView.removeFooterView(footer);
                loadFinishFlag = true;
            }
        }
    };
}

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

下面是运行结果:


在加载20条:

再加载20条:


发布了613 篇原创文章 · 获赞 315 · 访问量 187万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览