上拉加载页面
package com.example.listviewloaddatademo; 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(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }