两个部分:(代码块和接口样式)
package com.example.admin.test1;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lv;
private List list;
private List listshow;
private boolean isGetData;
private int startIndex;
private int QUERY_COUT = 13;
private boolean last_load;
private int last;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (startIndex == 0) {
lv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
isGetData = false;
}
};
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
init();
adapter = new MyAdapter();
}
private void initView() {
lv = (ListView) findViewById(R.id.lv);
}
private void init() {
list = new ArrayList();
listshow = new ArrayList();
getData();
startIndex = 0;
isGetData = false;
last_load = true;
setData();
setScroll();
}
private void setScroll() {
lv.setOnScrollListener(new MyScrollView());
}
class MyAdapter extends BaseAdapter {
private TextView tv;
private View view;
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
view = View.inflate(MainActivity.this, R.layout.item, null);
} else {
view = convertView;
}
tv = (TextView) view.findViewById(R.id.tv);
if (list.size() == position) {
}
tv.setText("这是第" + position + "个");
return view;
}
}
private void getData() {
for (int i = 0; i < 100; i++) {
listshow.add(i);
}
}
private void setData() {
if (startIndex == 0) {
for (int i = 0; i < QUERY_COUT; i++) {
list.add(listshow.get(i));
}
} else if (!last_load) {
for (int i = listshow.size() - last; i < listshow.size(); i++) {
list.add(listshow.get(i));
}
} else {
for (int i = startIndex; i < QUERY_COUT + startIndex; i++) {
list.add(listshow.get(i));
}
}
mHandler.sendEmptyMessage(1000);
}
private class MyScrollView implements AbsListView.OnScrollListener {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://空闲
break;
case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://滑动
int lastVisiblePosition = lv.getLastVisiblePosition();
int totalCount = listshow.size();
if (lastVisiblePosition == (list.size() - 1) && !isGetData) {
last = totalCount - list.size();
if (last > QUERY_COUT) {
} else {
last_load = false;
}
if (list.size() == totalCount && last_load) {
Toast.makeText(MainActivity.this, "已经到最后一页", Toast.LENGTH_LONG).show();
} else {
isGetData = true;
startIndex += QUERY_COUT;
setData();
}
}
break;
case AbsListView.OnScrollListener.SCROLL_STATE_FLING://
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
}
}
代码只是提供一种思路,看明白思路写的时候好些
服务器接口一般是?limit=3&offset=3
前者limit一页显示多少后者offset是从哪里开始显示