分页原理:
1、设置setOnScrollListener监听滑动到底部
// 设置listview的滚动监听
// 设置分页
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
// 滚动停止(不一定到了底部)
if (isBottom) {
lv.addFooterView(footer);
handler.postDelayed(new Runnable() {
@Override
public void run() {
getNextData();
lv.removeFooterView(footer);
}
}, 3000);
}
break;
case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
break;
case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
isBottom = ((firstVisibleItem + visibleItemCount) == totalItemCount);
}
});
2、lv.addFooterView和lv.removeFooterView的使用
lv.addFooterView(footer);
lv.setAdapter(adapter);
lv.removeFooterView(footer);// 移除掉
3、更新完数据刷新界面
notifyDataSetChanged();
示例demo一:
package com.example.administrator.mylistviewpager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivityIIIII extends AppCompatActivity {
@BindView(R.id.listView_MainActivity)
ListView lv;
private ArrayList<String> data;
private MyBaseAdapter adapter;
private View footer;//加载视图:progressbar和textview提示
private static final int SUCCESS_GET_DATA = 0;
private boolean finish = true; // 是否加载完成
private boolean isBottom;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainiiii);
ButterKnife.bind(this);
adapter = new MyBaseAdapter();
//加载时提示
footer = View.inflate(getApplicationContext(), R.layout.footer, null);
// 这里必须添加页脚让setAdapter里面满足
// if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0)条件
lv.addFooterView(footer);
lv.setAdapter(adapter);
lv.removeFooterView(footer);// 移除掉
// 设置listview的滚动监听
// 设置分页
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:// 滚动停止(不一定到了底部)
if (isBottom) {
lv.addFooterView(footer);
handler.postDelayed(new Runnable() {
@Override
public void run() {
getNextData();
lv.removeFooterView(footer);
}
}, 3000);
}
break;
case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
break;
case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
isBottom = ((firstVisibleItem + visibleItemCount) == totalItemCount);
}
});
}
public void getNextData() {
// 得到当前的记录数
int count = adapter.getCount();
List<String> datas = new ArrayList<String>();
for (int i = count; i < count + 20; i++) {
datas.add("item" + i);
}
adapter.addData(datas);
}
//处理数据并绑定到view上
class MyBaseAdapter extends BaseAdapter {
private List<String> list = new ArrayList<String>(); //1. 集合 : 数据源
// 第一次初始化,先加载第一屏幕,到19就开始加载
public MyBaseAdapter() { //2. 构造方法
for (int i = 0; i < 20; i++) {
list.add("item" + i);
}
}
@Override
public int getCount() {
return this.list.size();
}
@Override
public Object getItem(int position) {
return this.list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertview, ViewGroup parent) { //3. 重写getView等方法
if (convertview == null) {
convertview = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null);
}
// 默认系统布局android.R.layout.simple_list_item_1 ,id为android.R.id.text1
TextView textview = (TextView) convertview.findViewById(android.R.id.text1);
textview.setText(this.list.get(position));
return convertview;
}
public void addData(List<String> items) {
this.list.addAll(items);
notifyDataSetChanged();
}
}
}
主界面布局:
<?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">
<ListView
android:id="@+id/listView_MainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light" />
</RelativeLayout>
footer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="正在加载数据..........."
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>
示例二:
LoadListView
public class LoadListView extends ListView implements AbsListView.OnScrollListener {
private boolean isLoading;//正在加载
private OnLoadListener onLoadListener;
private boolean canLoad;
private LinearLayout footerContainer;
public LoadListView(Context context) {
this(context, null);
}
public LoadListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LoadListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View footerLayout = LayoutInflater.from(context).inflate(R.layout.footer_layout, this, false);
footerContainer = ((LinearLayout) footerLayout.findViewById(R.id.footer_container));
footerContainer.setVisibility(View.GONE);
this.addFooterView(footerContainer);
this.setOnScrollListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//到达底部且多于一页
canLoad = ((firstVisibleItem + visibleItemCount) == totalItemCount) && visibleItemCount < totalItemCount;
if (canLoad && footerContainer.getVisibility() == GONE) {
footerContainer.setVisibility(View.VISIBLE);
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (canLoad && scrollState == SCROLL_STATE_IDLE) {
if (!isLoading) {
isLoading = true;
footerContainer.setVisibility(View.VISIBLE);
onLoadListener.onLoad();
}
}
}
public void loadComplete() {
isLoading = false;
footerContainer.setVisibility(View.GONE);
}
public void setOnLoadListener(OnLoadListener onLoadListener) {
this.onLoadListener = onLoadListener;
}
interface OnLoadListener {
void onLoad();
}
}
LoadActivity
public class LoadActivity extends AppCompatActivity {
private ArrayList<String> lists;
private LoadListView lv;
private ArrayAdapter<String> adapter;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_act);
lists = new ArrayList<>();
addDatas(lists);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, lists);
lv = ((LoadListView) this.findViewById(R.id.lv));
lv.setAdapter(adapter);
lv.setOnLoadListener(new LoadListView.OnLoadListener() {
@Override
public void onLoad() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
addDatas(lists);
adapter.notifyDataSetChanged();
lv.loadComplete();
}
}, 500);
}
});
}
private void addDatas(ArrayList<String> lists) {
int nextInt = new Random().nextInt(100);
for (int i = 0; i < 15; i++) {
lists.add("" + nextInt + "我是第" + i + "条");
}
}
}
load_act.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">
<com.joanzapata.android.pulldown.LoadListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:divider="#ff0000"
android:dividerHeight="5dp" />
</RelativeLayout>
footer_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/yellow"
android:gravity="center"
android:orientation="horizontal">
<ProgressBar
android:layout_width="30dp"
android:layout_height="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载数据..........."
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>