listview分页加载数据

1.java代码
//借鉴http://blog.csdn.net/cjjky/article/details/6898871

package com.example.listviews;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnScrollListener {

private List<String> list = new ArrayList<String>();
private ListView listView;
private View view;
private Button button;
private Handler handler = new Handler();
private MyBaseadapter baseadapter;
private int Nextsize = 100;
private int index = 0; //最后的可视项索引
private int ItemCount; //当前可视
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
view = getLayoutInflater().inflate(R.layout.login, null);
button = (Button) view.findViewById(R.id.button);
baseadapter = new MyBaseadapter();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
button.setText("正在加载");
handler.postDelayed(new Runnable() {

@Override
public void run() {
login();
baseadapter.notifyDataSetInvalidated();
button.setText("查看更多");

}
}, 2000);

}
});
listView.addFooterView(view);
initializeAdapter();
listView.setAdapter(baseadapter);
listView.setOnScrollListener(this);
}



//当count+10的item小于Nextsize就执行if否则就是else
private void login() {
int count = baseadapter.getCount();
if (count+10<Nextsize) {
for (int i = count+1; i <=count+10; i++) {
list.add("愿得一人心"+i);
}
}
else{
for (int i = count+1; i <=Nextsize; i++) {
list.add("愿得一人心"+i);
}
}

}


//每次加载的item
private void initializeAdapter(){
for(int i=1;i<=10;i++){
list.add("愿得一人心"+i);
}

}

private class MyBaseadapter extends BaseAdapter{

@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
TextView textView;
// TODO Auto-generated method stub
arg1 = getLayoutInflater().inflate(R.layout.item, null);
textView = (TextView) arg1.findViewById(R.id.textview);
textView.setText(list.get(arg0));
return arg1;
}

}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int itemsLastIndex = baseadapter.getCount()-1; //数据集最后一项的索引
int lastIndex = itemsLastIndex + 1;
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& index == lastIndex) {
// 如果是自动加载,可以在这里放置异步加载数据的代码
}
}


@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.ItemCount = visibleItemCount;
index = firstVisibleItem + visibleItemCount - 1;

Log.e("========================= ","========================");
Log.e("firstVisibleItem = ",firstVisibleItem+"");
Log.e("visibleItemCount = ",visibleItemCount+"");
Log.e("totalItemCount = ",totalItemCount+"");
Log.e("========================= ","========================");

//如果所有的记录选项等于数据集的条数,则移除列表底部视图
if(totalItemCount == Nextsize+1){
listView.removeFooterView(view);
Toast.makeText(this, "数据全部加载完!", Toast.LENGTH_LONG).show();
//隐藏button
button.setVisibility(View.GONE);
}
}
}
2.xml文件
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

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

</RelativeLayout>
3.底部button
<?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:orientation="vertical" >

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加载"
/>

</LinearLayout>
4.item
<?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:orientation="vertical" >

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

0 个评论

要回复文章请先登录注册

Template error, template file not found