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

2020-12-13 15:17

阅读:286

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

简单说下实现方法:

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

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

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

soscw.com,搜素材

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 
代码下载


评论


亲,登录后才可以留言!