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

Android 快速實作一個通用 EmptyView 空視圖

編輯:關於 Android 程式設計

好的 App 應該具備良好的互動,最好能貼心滿足使用者需求,而人性化的提醒正是其中一環。我們常在某些 App 看到這樣的場景:當內容載入失敗或抓取資料失敗時,畫面會切換成一個可與使用者互動的狀態,允許使用者點擊螢幕或某個按鈕,嘗試重新抓取內容或檢查網路連線等等。

Android 的 ListView 提供了 setEmptyView(...) 這類方法,當清單沒有資料時就會顯示該 emptyView。但並非所有 View 都有這樣的介面,於是我們可以自己實作一個通用的 emptyView。

以下透過簡單範例提供一種思路(当然不是唯一解),你可以依自己的創意做出各種樣式的 emptyView,本質上仍是呼叫 view.setVisibility(...)。效果如下圖,看起來相當簡潔吧!

廢話不多說,直接上程式碼!將 EmptyView 封裝在常用的 RelativeLayout 裡(當然也可以改用 LinearLayout 等)。

 public class CustomRelativeLayout extends RelativeLayout{ public static interface RetryListener{ void retry(); } private ProgressBar progressBar = null; private Button btn_refresh = null; private TextView tv_tip = null; private boolean isNormal = true; private RetryListener retryListener = null; private final static int tvTipId = 0x1001; public CustomRelativeLayout(Context context) { this(context, null); } public CustomRelativeLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * 設置加載界面 */ protected void setInProgress() { if(progressBar == null) { int size = (int) TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics()); progressBar = new ProgressBar(getContext()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(size,size); lp.addRule(RelativeLayout.CENTER_IN_PARENT); progressBar.setLayoutParams(lp); addView(progressBar); } if(getChildCount() > 0) { int childCount = getChildCount(); if(childCount >0) { for(int i=(childCount-1); i>=0; i--) { getChildAt(i).setVisibility(View.GONE); } } } if(btn_refresh != null) btn_refresh.setVisibility(View.GONE); if(tv_tip != null) tv_tip.setVisibility(View.GONE); progressBar.setVisibility(View.VISIBLE); } /** * 顯示加載失敗界面,隱藏所有正常的View元素 */ protected void setChildrenGone() { if(btn_refresh == null) { btn_refresh = new Button(getContext()); tv_tip = new TextView(getContext()); tv_tip.setText(網絡連接失敗); tv_tip.setGravity(Gravity.CENTER); tv_tip.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); btn_refresh.setText(刷新); btn_refresh.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(retryListener != null) { retryListener.retry(); } else { Toast.makeText(getContext(), 暫無數據, Toast.LENGTH_SHORT).show(); } } }); int btnWidth = (int)TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(btnWidth, LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.CENTER_IN_PARENT); lp.setMargins(0, 10, 0, 0); btn_refresh.setLayoutParams(lp); btn_refresh.setId(tvTipId); btn_refresh.setVisibility(View.GONE); addView(btn_refresh); RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); lp_tv.addRule(RelativeLayout.ABOVE, tvTipId); tv_tip.setLayoutParams(lp_tv); addView(tv_tip); } if(getChildCount() > 0) { int childCount = getChildCount(); if(childCount >0) { for(int i=(childCount-1); i>=0; i--) { getChildAt(i).setVisibility(View.GONE); } } } if(progressBar != null) progressBar.setVisibility(View.GONE); isNormal = false; btn_refresh.setVisibility(View.VISIBLE); tv_tip.setVisibility(View.VISIBLE); } /** * 顯示正常View元素 */ protected void setChildrenVisible() { if(getChildCount() > 0) { int childCount = getChildCount(); if(childCount >0) { for(int i=(childCount-1); i>=0; i--) { getChildAt(i).setVisibility(View.VISIBLE); } } if(progressBar != null) progressBar.setVisibility(View.GONE); if(btn_refresh != null) btn_refresh.setVisibility(View.GONE); if(tv_tip != null) tv_tip.setVisibility(View.GONE); isNormal = true; } } protected boolean isNormalView() { return isNormal; } public void setRetryListener(RetryListener listener) { this.retryListener = listener; } }

這裡特別提供了一個 RetryListener 介面,作為點擊「刷新」按鈕後的事件回呼。

接著模擬「載入中並載入失敗」的畫面,點擊按鈕後延遲 3 秒,假設抓取資料成功、更新 view 後顯示正常畫面:

 public class MainActivity extends Activity { private Button btn_start = null; private static CustomRelativeLayout cuLayout = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private static Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if(msg.what == 0x1) { cuLayout.setChildrenVisible(); } else if(msg.what == 0x2) { cuLayout.setChildrenGone(); } super.handleMessage(msg); } }; private void init() { btn_start = (Button) findViewById(R.id.btn_start); cuLayout = (CustomRelativeLayout) findViewById(R.id.container); cuLayout.setRetryListener(new CustomRelativeLayout.RetryListener(){ public void retry() { cuLayout.setInProgress(); mHandler.sendEmptyMessageDelayed(0x1,3000); } }); btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cuLayout.setChildrenGone(); } }); cuLayout.setInProgress(); mHandler.sendEmptyMessageDelayed(0x2,2000); } }

佈局檔:

   

就是這麼簡單!相信看完之後,你也能客製出自己喜歡的樣式。另外要提醒的是,作為 EmptyView 與載入中的 view,並不建議使用太複雜的元素,簡潔而美觀就足夠了。畢竟它僅是輔助角色,輕量化、幾乎不佔記憶體才是前提。

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