Android開(kāi)發(fā)實(shí)現(xiàn)自定義新聞加載頁(yè)面功能實(shí)例
本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)自定義新聞加載頁(yè)面功能。分享給大家供大家參考,具體如下:
一、概述:
1、效果演示:

2、說(shuō)明:在新聞頁(yè)面剛加載的時(shí)候,一般會(huì)出現(xiàn)五種狀態(tài)
未知狀態(tài)(STATE_UNKNOW)、空狀態(tài)(STATE_EMPTY)、加載中(STATE_LOADING)、錯(cuò)誤(STATE_ERROT)、成功(STATE_SUCCESS)
因?yàn)槊總€(gè)Detail頁(yè)面都會(huì)出現(xiàn),所以我們可以把他們封裝成一個(gè)LoadPage的自定義view,可以復(fù)用
二、實(shí)現(xiàn):
1、首先的定義三個(gè)布局,為什么是三個(gè),因?yàn)閡nkonw與loading的頁(yè)面可以使用同一個(gè),而success的頁(yè)面是加載數(shù)據(jù)的頁(yè)面,這里不用定義
1)loading頁(yè)面布局,只有一個(gè)進(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)空頁(yè)面只有一張圖片,顯示沒(méi)有數(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)錯(cuò)誤頁(yè)面有一張錯(cuò)誤圖片與按鈕,點(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ā)生錯(cuò)誤,點(diǎn)擊重新加載
Button btnError = (Button) mErrorView.findViewById(R.id.page_bt);
btnError.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
show();
}
});
showPages();
}
5、全部代碼:
/**
* @描述 加載頁(yè)面
* @項(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; // 空頁(yè)面
private View mErrorView; // 網(wǎng)絡(luò)錯(cuò)誤
private View mSuccessView; // 加載成功后的頁(yè)面
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ā)生錯(cuò)誤,點(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() {
//加載頁(yè)面顯示與不顯示
mLoadingView.setVisibility(currentState == STATE_UNKNOW || currentState == STATE_LOADING ? View.VISIBLE
: View.GONE);
//空頁(yè)面
mEmptyView.setVisibility(currentState == STATE_EMPTY ? View.VISIBLE : View.GONE);
//錯(cuò)誤頁(yè)面顯示
mErrorView.setVisibility(currentState == STATE_ERROT ? View.VISIBLE : View.GONE);
//如果數(shù)據(jù)加載成功了,
if (currentState == STATE_SUCCESS) {
if (mSuccessView == null) {
//加載成功頁(yè)面信息,成功后的頁(yè)面就是新聞頁(yè)面信息
mSuccessView = createSuccessView();
//添加頁(yè)面到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;
}
// 請(qǐng)求服務(wù)器 獲取服務(wù)器上數(shù)據(jù) 進(jìn)行判斷
// 請(qǐng)求服務(wù)器 返回一個(gè)結(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();
/**
* 顯示加載頁(yè)面
*/
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開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)雅虎新聞?wù)虞d視差動(dòng)畫(huà)效果
- Android仿網(wǎng)易新聞圖片詳情下滑隱藏效果示例代碼
- Android UI設(shè)計(jì)與開(kāi)發(fā)之PopupWindow仿騰訊新聞底部彈出菜單
- Android RecyclerView仿新聞?lì)^條的頻道管理功能
- Android網(wǎng)絡(luò)編程之簡(jiǎn)易新聞客戶端
- Android模擬實(shí)現(xiàn)網(wǎng)易新聞客戶端
- Android 模擬新聞APP顯示界面滑動(dòng)優(yōu)化實(shí)例代碼
- Android實(shí)現(xiàn)基本功能的新聞應(yīng)用
相關(guān)文章
Android面向單Activity開(kāi)發(fā)示例解析
這篇文章主要為大家介紹了Android面向單Activity開(kāi)發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android學(xué)習(xí)筆記——Menu介紹(一)
Android3.0(API level 11)開(kāi)始,Android設(shè)備不再需要專門(mén)的菜單鍵。隨著這種變化,Android app應(yīng)該取消對(duì)傳統(tǒng)6項(xiàng)菜單的依賴。取而代之的是提供anction bar來(lái)提供基本的用戶功能2014-10-10
解決Android Studio一直停留在MyApplication:syncing的問(wèn)題
這篇文章主要介紹了Android Studio一直停留在MyApplication:syncing的完美解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Android保持屏幕常亮2種實(shí)現(xiàn)方法
這篇文章主要介紹了Android保持屏幕常亮2種實(shí)現(xiàn)方法,本文分別給出示例代碼,需要的朋友可以參考下2015-05-05
Android仿微信雷達(dá)掃描效果的實(shí)現(xiàn)方法
最近看了一個(gè)視頻講了一種微信雷達(dá)掃描的實(shí)現(xiàn)方案,借鑒了一下,自己也寫(xiě)一個(gè)玩玩,所以下面這篇文章主要給大家介紹了利用Android模仿微信雷達(dá)掃描效果的實(shí)現(xiàn)方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-06-06
Android基于API的Tabs3實(shí)現(xiàn)仿優(yōu)酷t(yī)abhost效果實(shí)例
這篇文章主要介紹了Android基于API的Tabs3實(shí)現(xiàn)仿優(yōu)酷t(yī)abhost效果,結(jié)合完整實(shí)例形式分析了Android實(shí)現(xiàn)優(yōu)酷界面效果的相關(guān)技巧,需要的朋友可以參考下2015-12-12
android studio安裝時(shí) AVD出現(xiàn)問(wèn)題如何快速解決
這篇文章主要介紹了安裝android studio時(shí) AVD出現(xiàn)問(wèn)題如何快速處理,其實(shí)解決方法也很簡(jiǎn)單,文中通過(guò)截圖的形式給大家及時(shí)的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03

