介紹
上一篇博文寫了一個通用的加載view,這篇在加載view的基礎(chǔ)在包裹一層就是LoadingLayout了,主要的目的是免去每次加載時要隱藏主內(nèi)容布局,然后加載成功之后顯示主內(nèi)容布局這些繁瑣操作。如果你還不了解loadingView,可以簡單的看一下上一篇博文:Android 自定義通用的loadingview,實現(xiàn)原理很簡單,就是LoadingLayout在包裹內(nèi)容層的基礎(chǔ)上,在代碼里添加loadingView作為第二個子view,所以不做過多講解,大家看完直接下載源碼參考。
LoadingLayout
This is a view for simplify the operation to loading
一個app加載數(shù)據(jù)通常是顯示加載狀態(tài),加載成功之后顯示主內(nèi)容視圖,如果是列表數(shù)據(jù)的話如ListView,GridView,RecyclerView一般就不用設(shè)置主內(nèi)容視圖隱藏了,
但是如果主視圖有些控件如TextView會帶效果而不是一片空白的,我們通常需要隱藏主視圖,在請求到數(shù)據(jù)之后回填數(shù)據(jù)并顯示主視圖,而這些事情在代碼里設(shè)置總是很麻煩,
該控件的目的就是為了簡化這些步驟。
特點
1、使用簡單,實現(xiàn)原理也簡單。
2、支持自定義各種視圖,只需要把你要顯示的視圖set進去即可
3、支持設(shè)置錯誤視圖點擊事件。
這里只是提供個思路,大家可以下載源碼去修改成最適合你的view。
使用
1、xml里聲明view,包裹在內(nèi)容視圖的外層。
<?xml version="1.0" encoding="utf-8"?>
<com.qiangyuyang.demo.widget.CommonLoadingLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loadingLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="主內(nèi)容"/>
</com.qiangyuyang.demo.widget.CommonLoadingLayout>
2、Java代碼里獲取控件并在合適的時候調(diào)用加載,加載失敗,加載成功等方法。
public class LoadingLayoutActivity extends AppCompatActivity {
protected CommonLoadingLayout mLoadingLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_loading_layout);
mLoadingLayout = (CommonLoadingLayout) findViewById(R.id.loadingLayout);
//設(shè)置錯誤視圖點擊重新加載事件
mLoadingLayout.setLoadingHandler(new CommonLoadingView.LoadingHandler() {
@Override
public void doRequestData() {
mLoadingLayout.load();
mLoadingLayout.postDelayed(new Runnable() {
@Override
public void run() {
mLoadingLayout.loadSuccess();
}
}, 3000);
}
});
//模擬加載網(wǎng)絡(luò)請求后出現(xiàn)錯誤
mLoadingLayout.load();
mLoadingLayout.postDelayed(new Runnable() {
@Override
public void run() {
mLoadingLayout.loadError();
}
}, 3000);
}
}
3、自定義加載、加載錯誤、等視圖。
ProgressBar progressBar = new ProgressBar(this);
this.mLoadingLayout.setLoadingView(progressBar);
TextView textView = new TextView(this);
textView.setText("加載失敗...");
this.mLoadingLayout.setLoadingErrorView(textView);
mLoadingLayout.load();
源碼下載
https://github.com/yissan/LoadingLayout
如果我的文章對你有幫助,請動動手指點個贊或關(guān)注,您的支持會是我永恒的動力。