学习笔记:ListView的动态加载。

XML比较简单就不放了,loadingView里只有ProgressBar和TextView,main_activity里只有一个ListView。

有关代码的解释都写在里边注释了。


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;

public class MyListView extends Activity implements
		OnScrollListener {
	//maxCount记录要放入ListView的数据总条数
	private int maxCount = 50;
	//lastItem记录ListView中最后一条数据的位置
	private int lastItem = 0;
	//loadingView用来存放读取数据时显示的View
	private View loadingView;
	private ListView listView;
	//handler用于刷新ListView,显示新加载的数据
	private final Handler handler = new Handler();
	private ListViewAdapter adapter = new ListViewAdapter();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);

		//初始化数据
		init();
	}
	
	class ListViewAdapter extends BaseAdapter {
		//count用于记录已加载的数据数量
		int count = 10;
		
		public int getCount() {
			return count;
		}
		public Object getItem(int position) {
			return position;
		}
		public long getItemId(int position) {
			return position;
		}
		public View getView(int position, View view, ViewGroup parent) {
			TextView mTextView;
			
			if (view == null) {
				mTextView = new TextView(MyListView.this);
			} else {
				mTextView = (TextView) view;
			}
			
			mTextView.setText("第  " + (position + 1) + " 条内容");
			mTextView.setTextSize(20f);
			mTextView.setGravity(Gravity.CENTER);
			mTextView.setHeight(60);

			return mTextView;
		}
	}
	
	public void init(){
		//用inflater获取我们loadingView的XML文件
		loadingView = getLayoutInflater().inflate(R.layout.load_layout, null);
		listView = (ListView)findViewById(R.id.listView1);
		//在当前显示listView的最后一项往下拉时,添加loadingView
		listView.addFooterView(loadingView);
		
		listView.setAdapter(adapter);
		//监听listView的滚动
		listView.setOnScrollListener(this);
	}
	
	public void onScroll(AbsListView view, int mFirstVisibleItem,
			int mVisibleItemCount, int mTotalItemCount) {
		//这里要减去1是因为我们往下拉listView会添加一项loadingView
		//而我们要的最后一项会因此变成倒数第二项,所以要减去1
		lastItem = mFirstVisibleItem + mVisibleItemCount - 1;
		//如果已经加载了所有数据,则移除loadingView,因为已经没有新数据加载了
		//这里用一个Toast提示用户已经加载到最后一项了
		if (adapter.count >= maxCount) {
			listView.removeFooterView(loadingView);
			Toast.makeText(MyListView.this, "没有新内容可以刷新了哦~", Toast.LENGTH_SHORT);
		}
	}
	
	public void onScrollStateChanged(AbsListView view, int mScrollState) {
		//同时满足停止滚动、最后一项等于已加载数据的最后一条的位置、数据未加载完全时
		//启用handler改变adapter中count的数量,从而加载新内容
		//然后刷新listView,显示新加载的内容
		if (lastItem == adapter.count
				&& mScrollState == OnScrollListener.SCROLL_STATE_IDLE) {
			if (adapter.count < maxCount) {
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						adapter.count += 10;
						adapter.notifyDataSetChanged();
						listView.setSelection(lastItem - 1);
					}
				}, 5000);
			}
		}
	}
}

你可能感兴趣的:(ListView,Android开发,listview动态加载)