Android 自定義通用的loadingview

来源:http://www.cnblogs.com/yangqiangyu/archive/2017/01/20/6321988.html
-Advertisement-
Play Games

介紹 好久沒有寫博客啦,最近在接近新年了,年前的工作都要收尾,所以特別忙,周末抽空寫了個通用的載入view,寫篇博客分享出來。 功能 1、顯示載入視圖,載入失敗的時候顯示載入失敗視圖,數據為空時顯示數據為空視圖,支持為失敗視圖設置點擊事件重新載入數據。 2、支持個性化設置,自定義設置 載入、失敗、空 ...


介紹

好久沒有寫博客啦,最近在接近新年了,年前的工作都要收尾,所以特別忙,周末抽空寫了個通用的載入view,寫篇博客分享出來。

功能

1、顯示載入視圖,載入失敗的時候顯示載入失敗視圖,數據為空時顯示數據為空視圖,支持為失敗視圖設置點擊事件重新載入數據。

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

先放一張效果圖壓壓驚

這裡寫圖片描述

實現

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

具體代碼

java 代碼

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方法的使用。

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方法設置進去即可。

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);
    }
}

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

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



您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • /** * 第一種Ajax提交方式 * 這種方式需要直接使用ext Ajax方法進行提交 * 使用這種方式,需要將待傳遞的參數進行封裝 * @return entitySearch.firstname:document.all.firstname.value, */function saveUser ...
  • moment非常強大,提供了很多時間方法的封裝,項目需要一個小倒計時的功能,網上找了很多不合適,決定自己寫一個,直接上代碼 用法: html頁面首先要引入moment才能使用我上面的方法 假設HTML里要放入倒計時的地方在這裡 頁面上可以寫js: 看官們外觀可以修改上述代碼去做出你自己的風格,以上內 ...
  • 最近剛做完一個移動端的項目,產品之無敵,過程之艱辛,我就不多說了,記錄下在這個項目中遇到的問題,以防萬一,雖然這些可能都是已經被N多前輩解決掉了的問題,也放在這裡,算是為自己漫漫前端路鋪了一顆小石子兒吧,也在文末留下自己未能解決的疑問,希望看到的朋友能解惑。 都知道做移動端的開發,在電腦上調試好了的 ...
  • 項目中經常用到三角形,現在給大家講下用純CSS寫的下三角實心圖形 其他方向的三角圖形自己調樣式哈,舉一反三,不要這麼懶嘛,自己弄才會進步。 ...
  • 這是翻譯的一篇文章,原文是: "3 New CSS Features to Learn in 2017" ,翻譯的不是很好,如有疑問歡迎指出。 新的一年,我們有一系列新的東西要學習。儘管CSS有很多新的特性,但有三個特性令我最激動併進行學習。 1. Feature Queries(特性查詢) 在這之 ...
  • 今天學習的新內容是側滑導航欄,我想大家肯定都比較熟悉了,因為這個效果在qq裡面也有,最近一直跟室友們玩的游戲是快速讓自己的頭像的點贊量上千。當然我的效果跟qq是沒有辦法比的,因為那裡面的功能是在是太強大了。下麵我來展示一下我做的效果截圖。 我做的界面有點醜,但是對比之前已經是有了很大的改觀了。想做這 ...
  • 解決的辦法:在Build Settings >Aplle LLVM8.0 - Language - Objectibe-C >Weak Reference In Manual Retain Release 設置為YES。 ...
  • let systemFont = UIFont.systemFontOfSize(14)let otherFont = UIFont(descriptor: systemFont.fontDescriptor(), size: 16)以下是錯誤形式:let systemFont = UIFont.s... ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...