Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android Listview分批加載+自動加載(改進版)(附源碼下載)

android Listview分批加載+自動加載(改進版)(附源碼下載)

編輯:關於Android編程

這次在代碼上比上一次改進了一些,並且加載完全部數據後會在lisview底部顯示一個“已加載全部“的textview。大家可以對比我的上一篇博客

android Listview分批加載+自動加載(附源碼下載)看看代碼有啥不同


直接上代碼:

public class TestForListviewActivity extends Activity implements
		OnScrollListener {
	private View mFooterView;
	private LinearLayout mloadingLinear;//正在加載時顯示的view
	private TextView mLoadFinishTextView;//加載全部數據後顯示的view
	private final int LOAD_STATE_IDLE=0;//沒有在加載,並且服務器上還有數據沒加載
	private final int LOAD_STATE_LOADING=1;//正在加載狀態
	private final int LOAD_STATE_FINISH=2;//表示服務器上的全部數據都已加載完畢
	private int loadState=LOAD_STATE_IDLE;//記錄加載的狀態
	private final int MAX_COUNT=15;//表示服務器總共有MAX_COUNT條數據
	private final int EACH_COUNT=10;//表示每次加載的條數
	private ListView mListview = null;
	private PaginationAdapter mAdapter;
	private Handler handler=new Handler();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mFooterView = getLayoutInflater().inflate(R.layout.loadmore, null);
		mloadingLinear=(LinearLayout) mFooterView.findViewById(R.id.loading_linear);
		mLoadFinishTextView=(TextView) mFooterView.findViewById(R.id.load_finish_textview);
		mListview = (ListView) findViewById(R.id.listview);
		mListview.addFooterView(mFooterView);// 設置列表底部視圖
		
		List news=new ArrayList();
		mAdapter = new PaginationAdapter(news);;
		mListview.setAdapter(mAdapter);
		//設置setOnScrollListener會自動調用onscroll方法。
		mListview.setOnScrollListener(this);
	}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		Log.i("onS", "firstVisibleItem"+firstVisibleItem+" visibleItemCount"+visibleItemCount+" totalItemCount"+totalItemCount);
		if (firstVisibleItem+visibleItemCount==totalItemCount) {
			if(loadState==LOAD_STATE_IDLE)
			{
				Log.i("onScroll", "firstVisibleItem"+firstVisibleItem+" visibleItemCount"+visibleItemCount+" totalItemCount"+totalItemCount);
				loadState=LOAD_STATE_LOADING;
				loadMore();
			}
		}

	}

	public void onScrollStateChanged(AbsListView arg0, int scrollState) {
		Log.i("onScrollStateChanged", scrollState+"");
	}
	
	private void loadMore(){
		//等待2秒之後才加載,模擬網絡等待時間為2s
		handler.postDelayed(new Runnable() {
			public void run() {
				loadData();
				mAdapter.notifyDataSetChanged();
				if (loadState==LOAD_STATE_FINISH) {
					//加載全部數據後在底部顯示一個“已加載全部”的textview
					mloadingLinear.setVisibility(View.GONE);
					mLoadFinishTextView.setVisibility(View.VISIBLE);
				}

			}
		},2000);
	}

	private void loadData() {
		int dataIndex;//要加載的數據的index(從0開始)
		int count = mAdapter.getCount();
		//如果服務端還有數據,則繼續加載更多
		for (dataIndex = count; dataIndex < Math.min(count+EACH_COUNT, MAX_COUNT); dataIndex++) {
			News item = new News();
			item.setTitle("Title" + dataIndex);
			item.setContent("This is News Content" + dataIndex);
			mAdapter.addNewsItem(item);
		}
		//如果服務器上的全部數據都已加載完畢
		if (dataIndex==MAX_COUNT) {
			loadState=LOAD_STATE_FINISH;
		}
		else {
			loadState=LOAD_STATE_IDLE;
		}
	}
	
	class PaginationAdapter extends BaseAdapter {

		List newsItems;

		public PaginationAdapter(List newsitems) {
			this.newsItems = newsitems;
		}

		public int getCount() {
			return newsItems==null?0:newsItems.size();
		}

		public Object getItem(int position) {
			return newsItems.get(position);
		}

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

		public void addNewsItem(News newsitem) {
			newsItems.add(newsitem);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(R.layout.list_item,
						null);
			}
			// 新聞標題
			TextView tvTitle = (TextView) convertView
					.findViewById(R.id.newstitle);
			tvTitle.setText(newsItems.get(position).getTitle());
			// 新聞內容
			TextView tvContent = (TextView) convertView
					.findViewById(R.id.newscontent);
			tvContent.setText(newsItems.get(position).getContent());

			return convertView;
		}
	}
}


改進版demo下載:源碼下載

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved