Android 自定義 View加載視圖之 LoadingLayout
介紹
上一篇博文寫了一個通用的加載view,這篇在加載view的基礎在包裹一層就是LoadingLayout了,主要的目的是免去每次加載時要隱藏主內容布局,然后加載成功之后顯示主內容布局這些繁瑣操作。實現原理很簡單,就是LoadingLayout在包裹內容層的基礎上,在代碼里添加loadingView作為第二個子view,所以不做過多講解,大家看完直接下載源碼參考。
LoadingLayout
This is a view for simplify the operation to loading
一個app加載數據通常是顯示加載狀態,加載成功之后顯示主內容視圖,如果是列表數據的話如ListView,GridView,RecyclerView一般就不用設置主內容視圖隱藏了,
但是如果主視圖有些控件如TextView會帶效果而不是一片空白的,我們通常需要隱藏主視圖,在請求到數據之后回填數據并顯示主視圖,而這些事情在代碼里設置總是很麻煩,
該控件的目的就是為了簡化這些步驟。
這里寫圖片描述
特點
1、使用簡單,實現原理也簡單。
2、支持自定義各種視圖,只需要把你要顯示的視圖set進去即可
這里寫圖片描述
3、支持設置錯誤視圖點擊事件。
這里只是提供個思路,大家可以下載源碼去修改成最適合你的view。
使用
1、xml里聲明view,包裹在內容視圖的外層。
<?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="主內容"/>
</com.qiangyuyang.demo.widget.CommonLoadingLayout>
2、Java代碼里獲取控件并在合適的時候調用加載,加載失敗,加載成功等方法。
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);
//設置錯誤視圖點擊重新加載事件
mLoadingLayout.setLoadingHandler(new CommonLoadingView.LoadingHandler() {
@Override
public void doRequestData() {
mLoadingLayout.load();
mLoadingLayout.postDelayed(new Runnable() {
@Override
public void run() {
mLoadingLayout.loadSuccess();
}
}, 3000);
}
});
//模擬加載網絡請求后出現錯誤
mLoadingLayout.load();
mLoadingLayout.postDelayed(new Runnable() {
@Override
public void run() {
mLoadingLayout.loadError();
}
}, 3000);
}
}</code></pre>
3、自定義加載、加載錯誤、等視圖。
ProgressBar progressBar = new ProgressBar(this);
this.mLoadingLayout.setLoadingView(progressBar);
TextView textView = new TextView(this);
textView.setText("加載失敗...");
this.mLoadingLayout.setLoadingErrorView(textView);
mLoadingLayout.load();</code></pre>
來自:http://www.jianshu.com/p/a2bd9386d7c8
本文由用戶 nusjj078r 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!