Android開發(fā)實(shí)現(xiàn)自定義新聞加載頁面功能實(shí)例
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)自定義新聞加載頁面功能。分享給大家供大家參考,具體如下:
一、概述:
1、效果演示:
2、說明:在新聞頁面剛加載的時候,一般會出現(xiàn)五種狀態(tài)
未知狀態(tài)(STATE_UNKNOW)、空狀態(tài)(STATE_EMPTY)、加載中(STATE_LOADING)、錯誤(STATE_ERROT)、成功(STATE_SUCCESS)
因?yàn)槊總€Detail頁面都會出現(xiàn),所以我們可以把他們封裝成一個LoadPage的自定義view,可以復(fù)用
二、實(shí)現(xiàn):
1、首先的定義三個布局,為什么是三個,因?yàn)閡nkonw與loading的頁面可以使用同一個,而success的頁面是加載數(shù)據(jù)的頁面,這里不用定義
1)loading頁面布局,只有一個進(jìn)度條
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ProgressBar style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </RelativeLayout>
2)空頁面只有一張圖片,顯示沒有數(shù)據(jù)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/ic_empty_page" /> </RelativeLayout>
3)錯誤頁面有一張錯誤圖片與按鈕,點(diǎn)擊按鈕重新加載數(shù)據(jù)
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <ImageView android:id="@+id/page_iv" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerHorizontal="true" android:scaleType="centerInside" android:src="@drawable/ic_error_page" /> <Button android:id="@+id/page_bt" android:layout_width="wrap_content" android:layout_height="34dp" android:layout_below="@id/page_iv" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:background="@drawable/btn_bg" android:ellipsize="end" android:paddingLeft="10dp" android:paddingRight="10dp" android:singleLine="true" android:text="@string/load_error" android:textColor="#ff717171" android:textSize="14dp" /> </RelativeLayout> </FrameLayout>
4、初始化控件
/** * 初始化加載三種布局 */ private void init() { mLoadingView = initView(R.layout.loadpage_loading); mEmptyView = initView(R.layout.loadpage_empty); mErrorView = initView(R.layout.loadpage_error); //如果發(fā)生錯誤,點(diǎn)擊重新加載 Button btnError = (Button) mErrorView.findViewById(R.id.page_bt); btnError.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { show(); } }); showPages(); }
5、全部代碼:
/** * @描述 加載頁面 * @項(xiàng)目名稱 App_Shop * @包名 com.android.shop.view * @類名 LoadingPage * @author chenlin * @date 2014年3月29日 下午8:49:39 */ public abstract class LoadingPage extends FrameLayout { private final static int STATE_UNKNOW = 0; private final static int STATE_LOADING = 1; private final static int STATE_ERROT = 2; private final static int STATE_EMPTY = 3; private final static int STATE_SUCCESS = 4; // 不能使用靜態(tài)的, private int currentState = STATE_UNKNOW; private View mLoadingView; // 加載 private View mEmptyView; // 空頁面 private View mErrorView; // 網(wǎng)絡(luò)錯誤 private View mSuccessView; // 加載成功后的頁面 private Context mContext; /** * 定義枚舉類型 */ public enum LoadResult { error(STATE_ERROT), empty(STATE_EMPTY), success(STATE_SUCCESS); int value; LoadResult(int value) { this.value = value; } public int getValue() { return value; } } public LoadingPage(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; init(); } public LoadingPage(Context context, AttributeSet attrs) { this(context, attrs, 0); } public LoadingPage(Context context) { this(context, null); } /** * 初始化加載三種布局 */ private void init() { mLoadingView = initView(R.layout.loadpage_loading); mEmptyView = initView(R.layout.loadpage_empty); mErrorView = initView(R.layout.loadpage_error); //如果發(fā)生錯誤,點(diǎn)擊重新加載 Button btnError = (Button) mErrorView.findViewById(R.id.page_bt); btnError.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { show(); } }); showPages(); } public View initView(int resId) { View view = View.inflate(mContext, resId, null); if (view != null) { this.addView(view, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); return view; } return null; } private void showPages() { //加載頁面顯示與不顯示 mLoadingView.setVisibility(currentState == STATE_UNKNOW || currentState == STATE_LOADING ? View.VISIBLE : View.GONE); //空頁面 mEmptyView.setVisibility(currentState == STATE_EMPTY ? View.VISIBLE : View.GONE); //錯誤頁面顯示 mErrorView.setVisibility(currentState == STATE_ERROT ? View.VISIBLE : View.GONE); //如果數(shù)據(jù)加載成功了, if (currentState == STATE_SUCCESS) { if (mSuccessView == null) { //加載成功頁面信息,成功后的頁面就是新聞頁面信息 mSuccessView = createSuccessView(); //添加頁面到framelayout里 addView(mSuccessView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); mSuccessView.setVisibility(View.VISIBLE); }else { mSuccessView.setVisibility(View.GONE); } } } public void show() { if (currentState == STATE_EMPTY || currentState == STATE_ERROT) { currentState = STATE_LOADING; } // 請求服務(wù)器 獲取服務(wù)器上數(shù)據(jù) 進(jìn)行判斷 // 請求服務(wù)器 返回一個結(jié)果 ThreadManager.getInstance().createLongPool().execute(new Runnable() { @Override public void run() { //從服務(wù)器加載數(shù)據(jù),得到返回的狀態(tài)信息 final LoadResult result = loadFromServer(); if (result != null) { Util.runOnUiThread(new Runnable() { @Override public void run() { currentState = result.getValue(); //顯示 showPages(); } }); } } }); showPages(); } public abstract View createSuccessView(); public abstract LoadResult loadFromServer(); }
三、使用:
/** * @描述 fragment * @項(xiàng)目名稱 App_Shop * @包名 com.android.shop.fragment * @類名 BaseFragment * @author chenlin * @date 2014年3月28日 下午10:33:59 */ public abstract class BaseFragment<T> extends Fragment { private LoadingPage mLoadingPage; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (mLoadingPage == null) { mLoadingPage = new LoadingPage(getActivity()){ @Override public View createSuccessView() { return BaseFragment.this.createSuccessView(); } @Override public LoadResult loadFromServer() { return BaseFragment.this.load(); } }; }else { ViewUtil.removeParent(mLoadingPage); } return mLoadingPage; } /*** * 創(chuàng)建成功的界面 * @return */ public abstract View createSuccessView(); /** * 從服務(wù)器得到結(jié)果嗎 * @return */ protected abstract LoadResult load(); /** * 顯示加載頁面 */ public void show(){ if (mLoadingPage != null) { mLoadingPage.show(); } } /**校驗(yàn)數(shù)據(jù) */ public LoadResult checkData(List<T> datas){ if (datas == null) { return LoadResult.error; }else { if (datas.size() == 0) { return LoadResult.empty; }else { return LoadResult.success; } } } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Flutter 實(shí)現(xiàn)進(jìn)度條效果
在一些上傳頁面炫酷的進(jìn)度條效果都是怎么實(shí)現(xiàn)的,今天小編通過本文給大家分享Flutter 一行代碼快速實(shí)現(xiàn)你的進(jìn)度條效果,感興趣的朋友一起看看吧2020-05-05Android 調(diào)用系統(tǒng)聯(lián)系人界面(添加聯(lián)系人,添加已有聯(lián)系人,編輯和修改)
這篇文章主要介紹了Android 調(diào)用系統(tǒng)聯(lián)系人界面(添加聯(lián)系人,添加已有聯(lián)系人,編輯和修改),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03Android單片機(jī)與藍(lán)牙模塊通信實(shí)例代碼
這篇文章主要介紹了Android單片機(jī)與藍(lán)牙模塊通信實(shí)例代碼,非常實(shí)用,特此分享給大家,需要的朋友可以參考下2016-05-05Android開發(fā)之ProgressBar字體隨著進(jìn)度條的加載而滾動
這篇文章主要介紹了Android開發(fā)之ProgressBar字體隨著進(jìn)度條的加載而滾動,需要的朋友可以參考下2017-09-09建造者模式_動力節(jié)點(diǎn)Java學(xué)院整理
建造者實(shí)現(xiàn)抽象類的所有未實(shí)現(xiàn)的方法,具體來說一般是兩項(xiàng)任務(wù),組建產(chǎn)品;返回組建好的產(chǎn)品2017-08-08Android中Glide實(shí)現(xiàn)超簡單的圖片下載功能
本篇文章主要介紹了Android中Glide實(shí)現(xiàn)超簡單的圖片下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03