scrollView自动加载数据demo
package combaidu.mylistsrollview;
import java.util.ArrayList;
import java.util.List;
import com.baidu.adapter.MyAdapter;
import com.baidu.vo.Goods;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
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 implements OnScrollListener {
private ListView lv;
private List<Goods> list;
private boolean isload=false;
private int count=1;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<Goods>();
init();
}
private void init() {
lv = (ListView) findViewById(R.id.lv);
getdata("");
adapter = new MyAdapter(MainActivity.this,list);
lv.setAdapter(adapter);
//设置滚动监听
lv.setOnScrollListener(this);
}
private void getdata(String param) {
for(int i=0;i<30;i++){
Goods goods=new Goods();
goods.setImage(R.drawable.ic_launcher);
goods.setText(param+"第"+i+"条目");
list.add(goods);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
isload = ((firstVisibleItem+visibleItemCount)==totalItemCount);
Log.d("tag", "first:"+firstVisibleItem+",visibleItemCount:"+visibleItemCount+",total:"+totalItemCount);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(isload&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){
Toast.makeText(getApplicationContext(), "开始加载数据", 0).show();
getdata("web"+count);
count++;
adapter.notifyDataSetChanged();
}
}
}