介紹
上一篇博文寫了一個(gè)通用的加載view镣煮,這篇在加載view的基礎(chǔ)在包裹一層就是LoadingLayout了集晚,主要的目的是免去每次加載時(shí)要隱藏主內(nèi)容布局,然后加載成功之后顯示主內(nèi)容布局這些繁瑣操作壶冒。如果你還不了解loadingView福青,可以簡(jiǎn)單的看一下上一篇博文:Android 自定義通用的loadingview,實(shí)現(xiàn)原理很簡(jiǎn)單绕辖,就是LoadingLayout在包裹內(nèi)容層的基礎(chǔ)上摇肌,在代碼里添加loadingView作為第二個(gè)子view,所以不做過多講解仪际,大家看完直接下載源碼參考围小。
LoadingLayout
This is a view for simplify the operation to loading
一個(gè)app加載數(shù)據(jù)通常是顯示加載狀態(tài),加載成功之后顯示主內(nèi)容視圖弟头,如果是列表數(shù)據(jù)的話如ListView吩抓,GridView,RecyclerView一般就不用設(shè)置主內(nèi)容視圖隱藏了赴恨,
但是如果主視圖有些控件如TextView會(huì)帶效果而不是一片空白的疹娶,我們通常需要隱藏主視圖,在請(qǐng)求到數(shù)據(jù)之后回填數(shù)據(jù)并顯示主視圖伦连,而這些事情在代碼里設(shè)置總是很麻煩雨饺,
該控件的目的就是為了簡(jiǎn)化這些步驟。
特點(diǎn)
1惑淳、使用簡(jiǎn)單额港,實(shí)現(xiàn)原理也簡(jiǎn)單。
2歧焦、支持自定義各種視圖移斩,只需要把你要顯示的視圖set進(jìn)去即可
3、支持設(shè)置錯(cuò)誤視圖點(diǎn)擊事件绢馍。
這里只是提供個(gè)思路向瓷,大家可以下載源碼去修改成最適合你的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代碼里獲取控件并在合適的時(shí)候調(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è)置錯(cuò)誤視圖點(diǎn)擊重新加載事件
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ò)請(qǐng)求后出現(xiàn)錯(cuò)誤
mLoadingLayout.load();
mLoadingLayout.postDelayed(new Runnable() {
@Override
public void run() {
mLoadingLayout.loadError();
}
}, 3000);
}
}
3长搀、自定義加載、加載錯(cuò)誤鸡典、等視圖源请。
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
如果我的文章對(duì)你有幫助,請(qǐng)動(dòng)動(dòng)手指點(diǎn)個(gè)贊或關(guān)注,您的支持會(huì)是我永恒的動(dòng)力巢钓。