Android 自訂通用的loadingview

來源:互聯網
上載者:User

標籤:i++   pos   參考   click   arraylist   length   err   tde   失敗   

介紹

好久沒有寫部落格啦,最近在接近新年了,年前的工作都要收尾,所以特別忙,周末抽空寫了個通用的載入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);    }}

至於具體的布局和樣式檔案就不貼了,主要是實現思路,代碼

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

Android 自訂通用的loadingview

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.