ApiDemos-->Views-lists-slow adapter学习

今天来按照apidemos提供的方法来实现slow loading的效果.

简单说下实现方法:

实现ListView.OnScrollListener ,监听到手势滑动的情况,当处于滚动状态时,将新显示的items 设置为Loading , 当离开屏幕时,才加载真实的数据.

设置数据时,要用到getFirstVisiblePosition属性来计算应该加载第几个item.

该小demo应该算是学习Android AsyncTask异步加载的基础.

ApiDemos-->Views-lists-slow adapter学习

Main.java

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
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 com.example.testmyviewslistsactivateitems.R;

/**
 * 
 * @author Administrator 仿效果slow loading apiDemos -- Views -Lists - Slow Adapter
 */
public class Main extends ListActivity {

	private boolean mBusy = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setListAdapter(new SlowAdapter(this));
		// 设置选择模式为单选
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		// 首次加载设置选中items
		getListView().setItemChecked(0, true);
		getListView().setOnScrollListener(new OnScrollListener());
	}

	protected class OnScrollListener implements ListView.OnScrollListener {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
			//The view is not scrolling.
			case OnScrollListener.SCROLL_STATE_IDLE:
				mBusy = false;
				int first = view.getFirstVisiblePosition();
				int count = view.getChildCount();
				for (int i = 0; i < count; i++) {
					TextView t = (TextView) view.getChildAt(i);
					if (t.getTag() != null) {
						t.setText(mStrings[first + i]);
						t.setTag(null);
					}
				}
				break;
			// The user is scrolling using touch, and their finger is still on the screen
			case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
				mBusy = true;
				break;
		    //The user had previously been scrolling using touch and had performed a fling. 
		   //The animation is now coasting to a stop
			case OnScrollListener.SCROLL_STATE_FLING:
				mBusy = true;
				break;
			}
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
		}

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		getListView().setItemChecked(position, true);
	}

	// 自定义适配器
	private class SlowAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public SlowAdapter(Context context) {
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mStrings.length;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView text;

			if (convertView == null) {
				text = (TextView) mInflater.inflate(R.layout.main, null, false);
			} else {
				text = (TextView) convertView;
			}

			if (!mBusy) {
				text.setText(mStrings[position]);
				text.setTag(null);
			} else {
				text.setText("Loading...");
				text.setTag(this);
			}
			return text;
		}

	}

	// data
	public static final String[] mStrings = { "Abbaye de Belloc",
			"Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
			"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu",
			"Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler",
			"Alverca", "Ambert", "American Cheese", "Ami du Chambertin",
			"Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell",
			"Vacherin-Fribourgeois", "Valencay", "Vasterbottenost", "Venaco",
			"Vendomois", "Vieux Corse", "Vignotte", "Vulscombe",
			"Waimata Farmhouse Blue", "Washed Rind Cheese (Australian)",
			"Waterloo", "Weichkaese", "Wellington", "Wensleydale",
			"White Stilton", "Whitestone Farmhouse", "Wigmore",
			"Woodside Cabecou", "Xanadu", "Xynotyro", "Yarg Cornish",
			"Yarra Valley Pyramid", "Yorkshire Blue", "Zamorano",
			"Zanetti Grana Padano", "Zanetti Parmigiano Reggiano" };
}

代码下载

ApiDemos-->Views-lists-slow adapter学习,布布扣,bubuko.com

ApiDemos-->Views-lists-slow adapter学习

上一篇:windows程序扫雷程序设计


下一篇:WPF 窗体拖转时不触发MouseLeftButtonUpEvent