Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

動手打造一個 Android 數據加載界面狀態切換的開源庫

編輯:關於 Android 程式設計

文章伊始,讓我們先靜心回憶三秒:在我們寫過的 Android 應用裡,是不是到處都離不開數據加載的需求?如果是,那麼先看下面這張圖:

接下來我們暫且忘掉自己是安卓開發者,而是站在一個不懂技術的 APP 使用者角度來交流。作為使用者,此時內心應該是懵逼的:打開應用進入第一個界面後發現空空如也。什麼鬼?手機斷網啦?一看網絡正常。反覆推測後得出結論:這應用真爛,刪掉刪掉。

切換回開發者身份分析:該界面本應有個 ListView 顯示從服務器獲取的數據,但由於網絡不暢、服務器異常等原因導致請求失敗、無數據返回,於是頁面空白。顯然,作為一個優秀的應用,用戶體驗是頭等大事,針對這種情況必須給用戶友好提示。

初步改進後的效果如下:

當請求未成功返回數據時及時向用戶交代,起碼讓用戶明白這次沒獲取到「數據」,這比之前友好多了。這是很常見但很有必要的做法,實現起來也很簡單——為 ListView 設置 setEmptyView 即可。

布局上,id 為 empty 的部分就是無數據時展示給用戶的 view:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="match_parent" android:layout_height="match_parent">
 <ListView android:id="@+id/lv_animals"
 android:layout_width="match_parent" android:layout_height="match_parent"/>
 <RelativeLayout android:id="@+id/empty"
 android:layout_width="match_parent" android:layout_height="match_parent">
 <ImageView android:id="@+id/empty_icon"
 android:layout_centerInParent="true"
 android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:background="@drawable/ic_empty"/>
 <TextView android:layout_below="@id/empty_icon"
 android:layout_centerHorizontal="true"
 android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:text="還沒有數據哦,親!"/>
 </RelativeLayout>
</RelativeLayout>

隨後在 Java 代碼中調用 setEmptyView 把指定 view 設置給 ListView 即可。這種做法早已不是新鮮事。那麼如何通過 SmartLoadingLayout 為界面加上不同的切換狀態呢?

我們採用一個簡單且具有代表性的布局:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="match_parent" android:layout_height="match_parent"
 android:orientation="vertical">
 <TextView android:id="@+id/tv_content"
 android:layout_centerInParent="true"
 android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:text="Hello Android!"/>
</LinearLayout>

通過 SmartLoadingLayout 為界面加上切換狀態:

public class MainActivity extends AppCompatActivity {
 private TextView tvContent;
 private DefaultLoadingLayout mLayout; // SmartLoadingLayout 對象
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 tvContent = (TextView) findViewById(R.id.tv_content);
 mLayout = SmartLoadingLayout.createDefaultLayout(this, tvContent);
 }
}

唯一的額外工作是需要一個 DefaultLoadingLayout 對象,它通過調用 SmartLoadingLayout 的靜態方法 createDefaultLayout 獲取。該方法需要兩個參數:宿主 Activity,以及作為內容的 view(此處為 TextView)。準備工作就這麼簡單。

接下來模擬等待加載並顯示的效果:

private Handler mHandler = new Handler(){
 @Override public void handleMessage(Message msg) {
 // 數據下載完成,轉換狀態,顯示內容視圖
 mLayout.onDone();
 }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
 //......
 mLayout.onLoading(); // 進入 loading 狀態
 new Thread(new Runnable() {
 @Override public void run() {
 downloadData(); // 模擬下載數據的耗時過程
 mHandler.sendEmptyMessage(1); // 下載完畢後通知 handler
 }
 }).start();
}

運行看效果:

可以看到,我們要做的就是開始加載時調用 onLoading,成功獲取數據後調用 onDone。

若這次進入時沒有獲取到數據(空視圖),把 onDone 改為 onEmpty 即可:

private Handler mHandler = new Handler(){
 @Override public void handleMessage(Message msg) { mLayout.onEmpty(); }
};

再次運行:

同理,若讀取數據過程中發生網絡異常,把 onEmpty 改為 onError:

error 狀態下通常會有一個按鈕用於再次發送請求刷新界面。那如何為這個按鈕添加操作?此外你可能還會有其它需求,比如覺得錯誤圖標太醜、想自定義描述信息、想換按鈕文案等等。以下代碼一次體現所有解決方式:

mLayout.setErrorButtonListener(new View.OnClickListener() {
 @Override public void onClick(View v) {
 mLayout.replaceErrorIcon(R.mipmap.ic_launcher);
 mLayout.setErrorDescription("This my error information.");
 mLayout.setErrorDescriptionColor(Color.BLUE);
 mLayout.setErrorDescriptionTextSize(20);
 mLayout.setErrorButtonText("oh!no!");
 mLayout.setErrorButtonTextColor(Color.RED);
 mLayout.setErrorButtonBackground(R.drawable.bg_error);
 }
});

再次運行看效果:

同理,若不喜歡 Loading、Empty 的效果,都可通過對應方法自定義風格。但若說「到目前為止所有的一切我都不喜歡」,那就用另一種類型 CustomLoadingLayout,所有界面都可完全自定義。

比如已在項目中定義好各狀態的布局文件,把它們 include 進來:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="match_parent" android:layout_height="match_parent"
 android:orientation="vertical">
 <ListView android:id="@+id/lv_content"
 android:layout_centerInParent="true"
 android:layout_width="match_parent" android:layout_height="match_parent">
 <!-- this is your custom part -->
 <include android:id="@+id/my_empty_page" layout="@layout/my_empty_page"/>
 <include android:id="@+id/my_error_page" layout="@layout/my_error_page"/>
 <include android:id="@+id/my_loading_page" layout="@layout/my_loading_page"/>
 </ListView>
</LinearLayout>

接著是代碼控制:

private CustomLoadingLayout mLayout;
mLayout = SmartLoadingLayout.createCustomLayout(this);
mLayout.setLoadingView(R.id.my_loading_page);
mLayout.setContentView(R.id.lv_content);
mLayout.setEmptyView(R.id.my_empty_page);
mLayout.setErrorView(R.id.my_error_page);

這樣就完成了準備工作,接下來同樣是在需要的時候調用 onLoading 等方法即可。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved