</pre><pre class="java" name="code">
public class MainActivity extends Activity {
private View footer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
private void initView() {
ListView lv = (ListView) findViewById(R.id.lv);
OtherFishService ofs = new OtherFishService(this);
/*
這裏調用數據庫,需要查詢得到數據,如果數據量很大,就很不好,所以需要優化
所以用到simplecursoradapter 作用 :僅加載需要顯示的數據,性能好,還可以自定義cursoradapter
List<Fish> fishs = ofs.queryAll();
MyBaseAdapter adapter = new MyBaseAdapter(this, fishs);
lv.setAdapter(adapter);*/
/* Cursor c = ofs.getCursor();
//簡單遊標適配器
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.item,
c,
new String[]{"_id","name","money"},
new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_money});
lv.setAdapter(adapter);
*/
//給ListView添加Footer
footer = getLayoutInflater().inflate(R.layout.footer, null);
lv.addFooterView(footer);
//設置控件的顯示和隱藏 GONE 隱藏並且不佔據空間 INVISIBLE 隱藏佔據空間 VISIBLE 顯示
footer.setVisibility(View.GONE);
Cursor c = ofs.getCursor();
MyCursorAdapter adapter = new MyCursorAdapter(this, c);
lv.setAdapter(adapter);
//添加滑動監聽事件
lv.setOnScrollListener(new MyOnScorllListener());
}
private class MyOnScorllListener implements OnScrollListener{
//ListView的狀態改變 idel --> scroll--> fling(很快的滑動)
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
/**
* AbsListView view, ListView
* int firstVisibleItem, listView屏幕控件的第一個條目的id
int visibleItemCount, 可見條目的總數
int totalItemCount 總共條目數
*/
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
Log.i("i", "firstVisibleItem:"+firstVisibleItem+",visibleItemCount:"+visibleItemCount+",totalItemCount:"+totalItemCount);
if(firstVisibleItem + visibleItemCount == totalItemCount){
//滑動到了最後
//添加正在加載數據
footer.setVisibility(View.VISIBLE);
}else{
footer.setVisibility(View.GONE);
}
}
}
}
public class MyCursorAdapter extends CursorAdapter {
private LayoutInflater mInflater;
public MyCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(context);
}
//創建item
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
View view = mInflater.inflate(R.layout.item, null);
return view;
}
//把數據綁定給View
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
//獲取條目的位置
int position = cursor.getPosition();
if(position%2==0){
view.setBackgroundColor(Color.RED);
}else{
view.setBackgroundColor(Color.YELLOW);
}
//獲取控件
TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_money = (TextView) view.findViewById(R.id.tv_money);
//獲取數據
String _id = cursor.getString(0);
String name = cursor.getString(1);
String money = cursor.getString(2);
//綁定數據
tv_id.setText(_id);
tv_name.setText(name);
tv_money.setText(money);
}
}