多状态布局《网络空白页》Two

前面已经写了一个封装好的,今天来一个自定义的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;
    }
}
相关文章
相关标签/搜索