scrollState有三种状态,分别是SCROLL_STATE_IDLE、SCROLL_STATE_TOUCH_SCROLL、SCROLL_STATE_FLING
*SCROLL_STATE_IDLE是当屏幕停止滚动时
*SCROLL_STATE_TOUCH_SCROLL是当用户在以触屏方式滚动屏幕并且手指仍然还在屏幕上时(The user is
scrolling using touch, and their finger is still on the screen)
*SCROLL_STATE_FLING是当用户由于之前划动屏幕并抬起手指,屏幕产生惯性滑动时
Mainactivity.java
package com.exmple.listviewdemoup; import java.util.ArrayList; import java.util.List; import com.exaple.vo.Apap; import com.exaple.vo.Super; 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.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity implements OnScrollListener { private boolean isLoad = false; int count = 1; private List<Super> list; private Apap ap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listview = (ListView) findViewById(R.id.listview); getData(""); ap = new Apap(list, MainActivity.this); listview.setAdapter(ap); listview.setOnScrollListener(this); } public List<Super> getData(String param) { list = new ArrayList<Super>(); for (int i = 0; i < 30; i++) { Super s = new Super(param + "fan" + i); list.add(s); } return list; } List<Super> lists = new ArrayList<Super>(); @Override public void onScrollStateChanged(AbsListView view, int scrollState) { 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(); getData("web" + count); count++; ap.getData(list); ap.notifyDataSetChanged(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { Log.i("TAG", "firstVisibleItem=" + firstVisibleItem); Log.i("TAG", "visibleItemCount=" + visibleItemCount); Log.i("TAG", "totalItemCount=" + totalItemCount); isLoad = ((firstVisibleItem + visibleItemCount) == totalItemCount); } }
适配器
package com.exaple.vo; import java.util.HashMap; import java.util.List; import com.exaple.vo.Super; import com.exmple.listviewdemoup.R; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.TextView; public class Apap extends BaseAdapter { List<Super> list; Context co; private View view; private ViewHolder vh; public Apap(List<Super> list, Context co) { super(); this.list = list; this.co = co; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { if (convertView == null) { view = View.inflate(co, R.layout.apa, null); vh = new ViewHolder(); vh.name = (TextView) view.findViewById(R.id.text); view.setTag(vh); } else { view = convertView; vh = (ViewHolder) view.getTag(); } vh.name.setText(list.get(position).getName()); return view; } public void getData(List<Super> s) { list.addAll(s); } public class ViewHolder { TextView name; TextView price; CheckBox cb; } }