//=========主页面=============
package com.example.fenye;
import java.util.ArrayList;
import java.util.List;
import com.example.fenye.adapter.MyAdapter;
import com.example.fenye.bean.News;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnScrollListener {
private List<News> news=new ArrayList<News>();
private ListView lv;
private View loadmoreView;
private Button bt_loadmore;
private int visibleLastIndex =0;//最后的可视项索引
private int visibleItemCount;// 当前窗口可见项总数
private int datasize=38;//模拟数据集的条数
private Handler handler=new Handler();
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到加载更多的布局
loadmoreView = getLayoutInflater().inflate(R.layout.loadmore, null);
//找到加载更多的控件
bt_loadmore = (Button) loadmoreView.findViewById(R.id.bt_loadmore);
//设置加载更多的监听
bt_loadmore.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
bt_loadmore.setText("正在加载中...");//设置按钮文字
//使用handler发送一个延迟消息
handler.postDelayed(new Runnable() {
@Override
public void run() {
//加载更多数据
loadMoreData();
//刷新适配器
adapter.notifyDataSetChanged();
bt_loadmore.setText("查看更多...");//恢复按钮文字
}
}, 2000);
}
});
//找到控件
lv = (ListView) findViewById(R.id.lv);
lv.addFooterView(loadmoreView);
//初始化数据
initializeAdapter();
//设置适配器
adapter = new MyAdapter(news, this);
lv.setAdapter(adapter);
lv.setOnScrollListener(this);
}
//加载更多数据
private void loadMoreData() {
int count=adapter.getCount();
if (count+10<datasize) {
for (int i = count+1; i <=count+10; i++) {
News item=new News();
item.setName("姓名:"+i);
adapter.addNewsItem(item);
}
}else{
for (int i = count+1; i <datasize; i++) {
News item=new News();
item.setName("姓名:"+i);
adapter.addNewsItem(item);
}
}
}
//初始化数据
private void initializeAdapter() {
for (int i = 0; i <=10; i++) {
News newss=new News();
newss.setName("姓名:"+i);
news.add(newss);
}
}
//滑动监听
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int itemsLaseIndex=adapter.getCount()-1;//数据集最后一项的索引
int lastIndex=itemsLaseIndex+1;
if (scrollState==OnScrollListener.SCROLL_STATE_IDLE&&visibleLastIndex==lastIndex) {
// 如果是自动加载,可以在这里放置异步加载数据的代码
}
}
//滑动监听
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
visibleItemCount=visibleItemCount;
visibleLastIndex=firstVisibleItem+visibleItemCount-1;
//如果所有的记录选项等于数据集的条数,则移除列表底部视图
if (totalItemCount==datasize+1) {
lv.removeFooterView(loadmoreView);
Toast.makeText(this, "数据全部加载完!", 0).show();
}
}
}
//========创建一个MyAdapter 继承 BaseAdapter====================
package com.example.fenye.adapter;
import java.util.List;
import com.example.fenye.R;
import com.example.fenye.bean.News;
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<News> news;
private Context context;
public MyAdapter(List<News> news, Context context) {
super();
this.news = news;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return news.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return news.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) {
ViewHolder holder;
News newss=news.get(position);
if (convertView==null) {
convertView=View.inflate(context, R.layout.list_item, null);
holder=new ViewHolder();
holder.name=(TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(holder);
}
holder=(ViewHolder) convertView.getTag();
holder.name.setText(newss.getName());
return convertView;
}
static class ViewHolder{
public TextView name;
}
public void addNewsItem(News newsitem){
news.add(newsitem);
}
}
//===========bean包==创建一个News类======================
package com.example.fenye.bean;
public class News {
private String name;
public News(String name) {
super();
this.name = name;
}
public News() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "News [name=" + name + "]";
}
}
//=============main布局文件 ==========================
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lv"></ListView>
</RelativeLayout>
//=========list_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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_name"/>
</LinearLayout>
//=========loadmore布局文件====================
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_loadmore"
android:text="查看更多..."/>
</LinearLayout>