ListView分页加载


import java.util.ArrayList;
import java.util.List;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnScrollListener{
	@ViewInject(R.id.lv)
	private ListView lv;
	private ArrayAdapter<String> adapter = null;
	
	private List<String> data = null;//保存数据
	//是否需要加载更多
	private boolean isLoad = false;
	
	int count = 1;
	
	@OnClick(R.id.lv)
	public void sa(View v){
		
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lv = (ListView)findViewById(R.id.lv);
		data = new ArrayList<String>();
		initData("");
		adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.list_pager_item,data);
		lv.setAdapter(adapter);
		lv.setOnScrollListener(this);
	}

    // "" 本地数据   web 网络数据
	private void initData(String param) {
		// TODO Auto-generated method stub
		for(int i=0;i<30;i++){
			data.add(param+" data"+i);
		}
	}

	//
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		//Log.i("aaa", "onScroll");
		Log.i("TAG", "firstVisibleItem="+firstVisibleItem);
		Log.i("TAG", "visibleItemCount="+visibleItemCount);
		Log.i("TAG", "totalItemCount="+totalItemCount);
		isLoad = ((firstVisibleItem+visibleItemCount)==totalItemCount);
		
	}

	
	
	/* (non-Javadoc)
	 * 
	 * SCROLL_STATE_FLING 手指做出抛的动作
	 * SCROLL_STATE_IDLE 停止滚动
	 * SCROLL_STATE_TOUCH_SCROLL 手指在屏幕上,屏幕滚动
	 * @see android.widget.AbsListView.OnScrollListener#onScrollStateChanged(android.widget.AbsListView, int)
	 */
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		//当ListView滚动到底部,并且ListView停止滚动
		Log.i("TAG", "onScrollStateChanged  isLoad="+isLoad+";是否是停止滚动:"+(scrollState==OnScrollListener.SCROLL_STATE_IDLE));
		Log.i("TAG", "scrollState"+scrollState);
		if(isLoad&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){
			Toast.makeText(getApplicationContext(), "开始加载数据", 1).show();
			initData("web"+count);
			count++;
			adapter.notifyDataSetChanged();
		}
		
		
	}

	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值