Android開發常用到的中間View(加載,重試等)

看圖:
這裏寫圖片描述
邏輯非常簡單,只是自定義一個簡單的Layout即可:

public class MiddleView extends RelativeLayout {


    RelativeLayout loadingLayout;
    RelativeLayout retryLayout;
    ImageView emptyView;
    Button btnRetry;

    RetryListener retryListener;

    public MiddleView(Context context) {
        super(context);
        init();
    }

    public MiddleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        View rootView = View.inflate(getContext(), R.layout.middle_view, null);
        loadingLayout = (RelativeLayout) rootView.findViewById(R.id.loading_layout);
        retryLayout = (RelativeLayout) rootView.findViewById(R.id.retry_layout);
        emptyView = (ImageView) rootView.findViewById(R.id.empty_layout);
        btnRetry = (Button) rootView.findViewById(R.id.btn_retry);
        LayoutParams rootViewParams = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rootViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(rootView,rootViewParams);
    }

    public void setRetryListener(RetryListener retryListener) {
        this.retryListener = retryListener;
    }

    public void success() {
        setVisibility(View.GONE);
    }

    public void empty() {
        setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.VISIBLE);
        loadingLayout.setVisibility(View.GONE);
        retryLayout.setVisibility(View.GONE);
    }

    public void loading() {
        setVisibility(View.VISIBLE);
        loadingLayout.setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.GONE);
        retryLayout.setVisibility(View.GONE);
    }

    public void retry() {
        setVisibility(View.VISIBLE);
        retryLayout.setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.GONE);
        loadingLayout.setVisibility(View.GONE);
        if (this.retryListener != null) {
            btnRetry.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    retryListener.retry();
                }
            });
        }
    }

    public interface RetryListener {
        void retry();
    }
}

middleview的佈局文件:

<?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">

    <RelativeLayout
        android:id="@+id/loading_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#80000000"
        android:padding="5dp"
        android:visibility="gone">

        <ProgressBar
            android:id="@+id/pb"
            style="?android:progressBarStyleInverse"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/pb"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:text="加載中..."
            android:textColor="#fff"
            android:textSize="16sp"/>
    </RelativeLayout>

    <ImageView
        android:id="@+id/empty_layout"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:src="@drawable/empty"
        android:visibility="gone"/>

    <RelativeLayout
        android:id="@+id/retry_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="5dp"
        android:visibility="gone">

        <ImageView
            android:id="@+id/img_retry"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/empty"/>

        <Button android:id="@+id/btn_retry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/img_retry"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="重試"
            android:textColor="#07f"
            android:textSize="16sp"/>
    </RelativeLayout>

</RelativeLayout>

可以將其寫在佈局文件中也可以在代碼中將其添加進去:
這裏寫圖片描述

發佈了36 篇原創文章 · 獲贊 7 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章