Code example for ViewSwitcher

Methods: addView, showNext

0
	/** 
	 * 以硬编码的方式来解决底部加载框。目前只能使用这种慵懒的实现手段... 
	 */ 
	private void initLoadFooter() 
	{ 
		loadSwitcher=new ViewSwitcher(getContext());
		// 
		loadSwitcher.addView(loadingView=initLoadingView());
		 
		loadSwitcher.addView(loadView=initLoadView());
		 
		if(triggerView==null)
			triggerView=loadView;//by default
	} 
	 
	public void setTriggerView(View triggerView) {
		this.triggerView = triggerView;
	} 
	 
	 
	/** 
	 * 子类覆盖此方法的时候必须要手动设置txtLoading 
	 * @return 
	 */ 
	protected View initLoadingView() {
		LinearLayout loadingLayout=new LinearLayout(getContext());
		loadingLayout.setGravity(Gravity.CENTER);loadingLayout.setOrientation(LinearLayout.HORIZONTAL);
		ProgressBar progressBar=new ProgressBar(getContext());
		progressBar.setIndeterminate(true);
		loadingLayout.addView(progressBar);
		TextView textView=new TextView(getContext());
		textView.setGravity(Gravity.CENTER);
		textView.setSingleLine(true);
		textView.setTextSize(17);
		textView.setPadding(12, 0, 0, 0);
		textView.setText(loadingMsg);
		loadingLayout.addView(textView);
		return loadingLayout;
	} 
	/** 
	 * 子类覆盖此方法的时候必须手动设置btnLoad 
	 * @return 
	 */ 
	protected View initLoadView(){
		Button button=new Button(getContext());
		button.setGravity(Gravity.CENTER);
		button.setTextSize(17);
		button.setText(loadButton);
		return button;
	} 
	 
	/** 
	 * 当列表到达尾端直接翻页 
	 * @author 13leaf 
	 * 
	 */ 
	final class AutoLoadScrollListener implements OnScrollListener
	{ 
 
		@Override 
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			 
		} 
 
		@Override 
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			if(loadFail) return;
			if(mPageManager.getLoadState()==PageManager.LOADING) return;
			if(firstVisibleItem+visibleItemCount==totalItemCount || view.getChildAt(visibleItemCount)==loadSwitcher)//翻页到底部
			{ 
				mPageManager.next();//触发翻页
			} 
		} 
		 
	} 
	 
	final class ListPageLoad implements PageLoadListener 
	{ 
 
		@Override 
		public void onPageLoading(int loadPageNo, int pageSum) {
			showLoadingFooter(); 
		} 
 
		@Override 
		public void onPageLoadComplete(int loadPageNo, int pageSum,
				Object content) {
			updateList(content);
			// 
			loadFail=false;
			//已经是最后一页了 
			if(loadPageNo>=pageSum)
			{ 
				removeFooterView(loadSwitcher);
			} 
			//重新设置状态 
			if(mFlag==AUTO_MODE)
				showLoadingFooter(); 
			else if(mFlag==MANUAL_MODE)
				 	showLoadedFooter(); 
		} 
 
		@Override 
		public void onPageLoadFail(int loadPageNo, int pageSum) {
			Toast.makeText(getContext(), loadErrorMsg,Toast.LENGTH_SHORT).show();
			loadFail=true;
			showLoadedFooter(); 
		} 
		 
	} 
	 
	/** 
	 * 更新列表,将读到的Page数据添加到列表中去。 
	 * @param currentContent 
	 */ 
	@SuppressWarnings("unchecked") 
	protected void updateList(Object currentContent)
	{ 
		if(adapter!=null)
			adapter.addPage(currentContent);
	} 
	 
	/** 
	 * 必须设置支持PageAdapter接口的适配器实例 
	 * issue:在添加了footerview后,adapter被绑定成了HeaderView。此时getAdapter将总是返回一个HeaderViewAdapter实例 
	 */ 
	@SuppressWarnings("rawtypes") 
	@Override 
	public void setAdapter(ListAdapter adapter) {
		//FIXED v1.0 当adapter为Null时应当传入,并且将adapter设置为空 
		if(adapter==null){
			super.setAdapter(null); 
			this.adapter=null;
		} 
		else if(adapter instanceof PageAdapter)
		{ 
			super.setAdapter(adapter);
			this.adapter=(PageAdapter) adapter;
		}else { 
			throw new RuntimeException("只支持PageAdapter接口。");
		} 
	} 
	 
	private void showLoadingFooter() 
	{ 
		loadView.setVisibility(INVISIBLE);
		if(!showFlag)
		{ 
			loadSwitcher.showPrevious();
			showFlag=true;
		} 
	} 
	 
	private void showLoadedFooter() 
	{ 
		loadView.setVisibility(VISIBLE);
		if(showFlag)
		{ 
			loadSwitcher.showNext();
			showFlag=false;
		} 
	} 
 
}