前面已經寫了一個封裝好的,今天來一個自定義的android
首先看下自定義頁面服務器
include_empty_material_view
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/vg_empty_container" android:layout_width="match_parent" android:background="@color/white_primary" android:layout_height="match_parent"> <!--催眠圓圈轉圈--> <com.nd.hy.android.commons.ui.CircularProgressBar xmlns:cpb="http://schemas.android.com/apk/res-auto" android:id="@+id/pb_empty_loader" android:layout_width="20dp" android:layout_height="20dp" android:layout_above="@+id/tv_empty" android:layout_centerInParent="true" android:layout_marginBottom="12dp" cpb:cui_progress_animation="false" cpb:cui_progress_color="@color/color_primary" cpb:cui_progress_rate="8" cpb:cui_radius="10dp" cpb:cui_ring_width="2dp" cpb:cui_rotate_rate="3" android:visibility="gone" /> <!--催眠圓圈轉圈--> <!--<com.nd.hy.android.commons.ui.CircularProgressBar--> <!--android:id="@+id/pb_empty_loader"--> <!--xmlns:cpb="http://schemas.android.com/apk/res-auto"--> <!--android:layout_width="20dp"--> <!--android:layout_height="20dp"--> <!--android:layout_marginBottom="5dp"--> <!--android:layout_centerHorizontal="true"--> <!--cpb:cui_progress_animation="false"--> <!--cpb:cui_progress_color="@color/color_primary"--> <!--cpb:cui_progress_rate="8"--> <!--cpb:cui_radius="10dp"--> <!--cpb:cui_ring_width="2dp"--> <!--cpb:cui_rotate_rate="3"/>--> <!--暫無信息--> <TextView android:id="@+id/tv_empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:drawablePadding="30dp" android:drawableTop="@drawable/ic_no_info" android:gravity="center" android:lineSpacingExtra="7dp" android:text="暫無信息" android:textColor="@color/gray_83" android:textSize="@dimen/font_16sp" /> <!--暫無信息--> </RelativeLayout>
XML佈局中調用ide
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/item_ly_pop_normal" android:descendantFocusability="blocksDescendants" android:id="@+id/homework_linear" android:orientation="vertical"> <!--標題--> <com.nd.hy.android.commons.ui.SimpleHeader android:id="@+id/simple_header" android:layout_width="match_parent" android:layout_height="@dimen/header_height" /> <!--標題--> <!--上拉下拉--> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/srl_content" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!--類listView控件--> <android.support.v7.widget.RecyclerView android:id="@+id/rv_content" android:layout_width="match_parent" android:layout_height="match_parent" /> <!--類listView控件--> <!--暫無數據--> <include layout="@layout/include_empty_material_view"/> <!--暫無數據--> </RelativeLayout> </android.support.v4.widget.SwipeRefreshLayout> <!--上拉下拉--> </LinearLayout>
記得要在RelativeLayout相對佈局中調用佈局
而後在代碼中設置post
@InjectView(R.id.vg_empty_container) RelativeLayout mRlEmpty;//暫無數據空佈局 @InjectView(R.id.pb_empty_loader) ProgressBar mPbEmptyLoader;//催眠圓圈轉圈 @InjectView(R.id.tv_empty) TextView mTvEmpty;//正在努力加載... private View footerView;//尾部 private TextView tvFooter;//尾部textView private ProgressBar pbFooter;//尾部轉圈 private RelativeLayout rlFooter;//尾部總體view private boolean isLoadingMore = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFooterView(); remoteData(); } /** * 實例化尾部 */ private void initFooterView() { footerView = LayoutInflater.from(getActivity()).inflate(R.layout.include_footer_view, null);//尾部 tvFooter = (TextView) footerView.findViewById(R.id.tv_foot_message);//尾部textView pbFooter = (ProgressBar) footerView.findViewById(R.id.pb_foot_loader);//尾部轉圈 rlFooter = (RelativeLayout) footerView.findViewById(R.id.footer_view);//尾部總體view } /** * 獲取服務器數據 */ private void remoteData() { if (client == null) { client = new AsyncHttpClient(); client.setTimeout(20000); } showLoading();//開始轉圈 client.post(ApiUrl.exam_list, requestParams, new AsyncHttpResponseHandler() { @Override public void onSuccess(int status, Header[] headers, byte[] response) { try { String str = new String(response); hideFooterLoading();//隱藏footer List<Examination> list = HttpTool.getExaminationList(str);//考試列表 if (list == null || list.size() == 0 && mDatas != null && mDatas.size() <= 0) { hideLoadingWithoutDelay();//提示空數據 return; } if (mDatas != null) { mDatas.addAll(list); totalCount = list.get(0).getTotalCount();//數據總數 } displayList(); } catch (Exception e) { e.printStackTrace(); } hideLoading();//結束轉圈 } @Override public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) { netWrong(); hideFooterLoading();//隱藏足部 if (arg3 instanceof BizException) { BizException bizException = (BizException) arg3; if (bizException.getCode() == 0) { if (mTvEmpty != null) { mTvEmpty.setText("加載失敗,點擊從新加載"); mTvEmpty.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher_background, 0, 0); } } } // showMessage(arg3.getMessage()); } }); } /** * 展現在加載中 */ private void showLoading() { showEmpty(); if (mTvEmpty != null) { mTvEmpty.setText("正在努力加載..."); mTvEmpty.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); } if (mPbEmptyLoader != null) { mPbEmptyLoader.setVisibility(View.VISIBLE); } } private void showEmpty() { if (mRlEmpty != null) { mRlEmpty.setVisibility(View.VISIBLE); } } /** * 隱藏足部加載 */ private void hideFooterLoading() { isLoadingMore = false; if (rlFooter != null) { rlFooter.setVisibility(View.INVISIBLE); } } private void hideLoadingWithoutDelay() { setEmptyView(); if (mPbEmptyLoader != null) { mPbEmptyLoader.setVisibility(View.GONE); } } private void displayList() { if (mDatas != null) { if (mIntermediary != null) { mIntermediary.setDatas(mDatas); } if (mAdapter != null) { mAdapter.notifyDataSetChanged(); } } else { //展現無數據 showEmpty(); } } /** * 隱藏在加載中 */ private void hideLoading() { new Handler().postDelayed(new Runnable() { @Override public void run() { if (mTvEmpty == null) { return; } setEmptyView(); if (mPbEmptyLoader != null) { mPbEmptyLoader.setVisibility(View.GONE); } hideEmpty(); ; } }, 300); } private void setEmptyView() { if (isAdded()) { if (mTvEmpty != null) { mTvEmpty.setText("暫無數據"); mTvEmpty.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher_background, 0, 0); } } } private void hideEmpty() { if (mRlEmpty != null) { mRlEmpty.setVisibility(View.GONE); } } private void netWrong() { if (mTvEmpty != null) { mTvEmpty.setText("加載失敗,點擊從新加載"); mTvEmpty.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher_background, 0, 0); } if (mPbEmptyLoader != null) { mPbEmptyLoader.setVisibility(View.GONE); } }
package com.nd.hy.android.commune.data.exception; import retrofit.RetrofitError; /** * BizException * * @author yangz * @version 2014/11/29 */ public class BizException extends RuntimeException { private static final String DEFAULT_MESSAGE = "操做失敗,請重試"; public static final int SENSITIVE_WORD_ERROR_CODE = 50004; private RetrofitError.Kind errorKind; private int code; private String message; public BizException() { } public RetrofitError.Kind getErrorKind() { return errorKind; } public BizException(String detailMessage, Throwable throwable) { super(detailMessage, throwable); this.message = detailMessage; } public BizException(String detailMessage) { super(detailMessage); this.message = detailMessage; } public BizException(Throwable throwable) { super(throwable); } public BizException(int code, String message) { this.code = code; this.message = message == null ? DEFAULT_MESSAGE : message; } public BizException(RetrofitError.Kind kind, String message) { this.errorKind = kind; this.message = message == null ? DEFAULT_MESSAGE : message; } public int getCode() { return code; } public String getMessage() { return message; } }