首页 > 代码库 > Android中的分页加载
Android中的分页加载
//----------------------MainActivity中---------------------------------------------------
package com.example.fenye;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
//每次加载的数量
private final int PAGE_NUMBER=15;
//index第几条目
private int index=0;
private List<String> list=new ArrayList<String>();
private ListView listview;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件Listview
listview = (ListView) findViewById(R.id.listview);
//设置Listview的滚动监听
listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//判断如果滚动停止时
if (scrollState==OnScrollListener.SCROLL_STATE_IDLE) {
//如果滚到到了最下面一行
if (view.getLastVisiblePosition()==list.size()-1) {
//从新赋值
index=index+PAGE_NUMBER;
//吐司提醒
Toast.makeText(MainActivity.this, "加载中...", 0).show();
//重新加载数据
getData();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
//加载数据
getData();
}
//加载数据
private void getData() {
new Thread(){
public void run() {
//index第几条目
for (int i = index; i < index+PAGE_NUMBER; i++) {
list.add("我是条目"+i);
}
};
}.start();
//每次加载的数量
try {
//延迟1秒
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//若果适配器为空,就创建适配器
if (adapter==null) {
adapter = new MyAdapter(list, this);
listview.setAdapter(adapter);
}else{
//刷新适配器
adapter.notifyDataSetChanged();
}
}
}
//------------------------main.xml布局文件中-----------------------------
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listview"></ListView>
//-------------------适配器----创建一个类继承----BaseAdapter---------------------------------------------
package com.example.fenye;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter{
private List<String> list;
private Context context;
public MyAdapter(List<String> list, Context context) {
super();
this.list = list;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView=View.inflate(context, R.layout.list_item, null);
}
TextView tv_name=(TextView) convertView.findViewById(R.id.tv_name);
tv_name.setText(list.get(position));
return convertView;
}
}
//-----------------list_item.xml布局文件中-----------------------------------------
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="http://www.mamicode.com/@drawable/ic_launcher"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_name"/>
//================以上是自动加载===========================================
//==============下面做一下手动加载=====只需要在=onCreate的方法中修改一下,如下:=================
/*
* 手动加载在Listview中用到listview.addFooterView(view),把button添加到Listview的最下面,
* 点击时,重新赋值和重新加载数据
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件Listview
listview = (ListView) findViewById(R.id.listview);
//找到button的布局
View view=getLayoutInflater().inflate(R.layout.button_footer, null);
//把控件button添加到Listview的下面
listview.addFooterView(view);
Button button=(Button) view.findViewById(R.id.button);
//加载数据
getData();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//重新赋值
index=index+PAGE_NUMBER;
//吐司提示
Toast.makeText(MainActivity.this, "加载中...", 0).show();
//加载数据
getData();
}
});
}
//---------------button_footer.xml布局文件中------------------
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="点击加载"/>
Android中的分页加载