Android 自定義通用的loadingview

介紹

很久沒有寫博客啦,最近在接近新年了,年前的工做都要收尾,因此特別忙,週末抽空寫了個通用的加載view,寫篇博客分享出來。java

功能android

一、顯示加載視圖,加載失敗的時候顯示加載失敗視圖,數據爲空時顯示數據爲空視圖,支持爲失敗視圖設置點擊事件從新加載數據。網絡

二、支持個性化設置,自定義設置 加載、失敗、空數據視圖。ide

先放一張效果圖壓壓驚佈局

這裏寫圖片描述

實現

實現思路其實就是一個FrameLayout裏添加三個佈局作處理顯示隱藏,自定義視圖其實就是替換裏面的view ,代碼比較簡單,若是直接看過個人自定義view系列文章,或者對自定義view有所瞭解,都很容易看懂,全部直接上代碼了。post

具體代碼

java 代碼this

public class CommonLoadingView extends FrameLayout {


    //加載時顯示文字
    protected TextView mLoadingTextTv;
    public Context mContext;
    //加載錯誤視圖
    protected LinearLayout mLoadErrorLl;
    //加載錯誤點擊事件處理
    private LoadingHandler mLoadingHandler;
    //加載view
    private View loadingView;
    //加載失敗view
    private View loadingErrorView;
    //數據爲空
    private View emptyView;


    public CommonLoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    public void setLoadingHandler(LoadingHandler loadingHandler) {
        mLoadingHandler = loadingHandler;
    }

    public void setLoadingErrorView(View loadingErrorView) {
        this.removeViewAt(1);
        this.loadingErrorView = loadingErrorView;
        this.loadingErrorView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLoadingHandler != null) {
                    mLoadingHandler.doRequestData();
                    CommonLoadingView.this.load();
                }
            }
        });
        this.addView(loadingErrorView,1);
    }

    public void setLoadingView(View loadingView) {
        this.removeViewAt(0);
        this.loadingView = loadingView;
        this.addView(loadingView,0);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        loadingView = inflate(mContext, R.layout.common_loading_view, null);
        loadingErrorView = inflate(mContext, R.layout.network_layout, null);
        emptyView = inflate(mContext, R.layout.empty_layout, null);
        this.addView(loadingView);
        this.addView(loadingErrorView);
        this.addView(emptyView);
        loadingErrorView.setVisibility(GONE);
        emptyView.setVisibility(GONE);
        initView(this);
    }


    public void setMessage(String message) {
        mLoadingTextTv.setText(message);
    }


    private void initView(View rootView) {
        mLoadingTextTv = (TextView) rootView.findViewById(R.id.loading_text_tv);
        mLoadErrorLl = (LinearLayout) rootView.findViewById(R.id.load_error_ll);
        mLoadErrorLl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLoadingHandler != null) {
                    CommonLoadingView.this.load();
                    mLoadingHandler.doRequestData();
                }
            }
        });
    }

    public void load(){
        loadingView.setVisibility(VISIBLE);
        loadingErrorView.setVisibility(GONE);
        emptyView.setVisibility(GONE);
    }

    public void load(String message){
        mLoadingTextTv.setText(message);
        loadingView.setVisibility(VISIBLE);
        loadingErrorView.setVisibility(GONE);
        emptyView.setVisibility(GONE);
    }


    public void loadSuccess(){
        this.loadSuccess(false);
    }

    public void loadSuccess(boolean isEmpty){
        loadingView.setVisibility(GONE);
        loadingErrorView.setVisibility(GONE);
        if (isEmpty) {
            emptyView.setVisibility(VISIBLE);
        }else{
            emptyView.setVisibility(GONE);
        }
    }


    public void loadError(){
        loadingView.setVisibility(GONE);
        loadingErrorView.setVisibility(VISIBLE);
    }


    public interface LoadingHandler{
        void doRequestData();
    }
}

使用

基本使用

幾個基本的 load loadError loadSucccess方法的使用。.net

public class DefaultViewActivity extends AppCompatActivity {

    protected ListView mListView;
    protected CommonLoadingView mLoadingView;
    private List<String> mList = new ArrayList<>();
    ArrayAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_default_view);
        initView();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.listView);
        mLoadingView = (CommonLoadingView) findViewById(R.id.loadingView);
        mLoadingView.load();

        //設置點擊錯誤視圖從新加載事件
        mLoadingView.setLoadingHandler(new CommonLoadingView.LoadingHandler() {
            @Override
            public void doRequestData() {
                mLoadingView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 1; i <=20 ; i++) {
                            mList.add(i+"");
                        }
                        adapter = new ArrayAdapter(DefaultViewActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, mList);
                        mListView.setAdapter(adapter);
                        mLoadingView.loadSuccess(false);
                    }
                },2500);
            }
        });

        //模擬網絡錯誤,加載失敗
        mLoadingView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mLoadingView.loadError();
            }
        },2500);


    }
}

自定義視圖 使用

只須要把本身自定義的view調用set方法設置進去便可。code

this.mLoadingView.setLoadingView(loadingView);

this.mLoadingView.setLoadingErrorView(loadingErrorView);
public class CustomViewActivity extends AppCompatActivity {

    protected ListView mListView;
    protected CommonLoadingView mLoadingView;
    private List<String> mList = new ArrayList<>();
    ArrayAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_default_view);
        initView();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.listView);
        mLoadingView = (CommonLoadingView) findViewById(R.id.loadingView);


        //設置自定義視圖
        ProgressBar progressBar = new ProgressBar(this);
        this.mLoadingView.setLoadingView(progressBar);
        TextView textView = new TextView(this);
        textView.setText("加載失敗...");
        this.mLoadingView.setLoadingErrorView(textView);

        mLoadingView.load();

        //設置點擊錯誤視圖從新加載事件
        mLoadingView.setLoadingHandler(new CommonLoadingView.LoadingHandler() {
            @Override
            public void doRequestData() {
                mLoadingView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 1; i <=20 ; i++) {
                            mList.add(i+"");
                        }
                        adapter = new ArrayAdapter(CustomViewActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, mList);
                        mListView.setAdapter(adapter);
                        mLoadingView.loadSuccess(false);
                    }
                },2500);
            }
        });

        //模擬網絡錯誤,加載失敗
        mLoadingView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mLoadingView.loadError();
            }
        },2500);
    }
}

至於具體的佈局和樣式文件就不貼了,主要是實現思路,代碼blog

下載請參考源碼下載,記得點贊喲!

相關文章
相關標籤/搜索