在项目中,都会用到ListView或GridView等列表控件。一般会用来展示从网络请求的数据 。如果请求的数据为空或者在请求的时候正好无没有网络了,我们的界面应该如何展示呢?数据为空的时候,ListView可以使用setEmptyView (View emptyView) 方法来我们需要的统一界面。数据加载失败呢?我们也可以统一进行处理。
002 | 
public class CommonShowView { | 
 
004 | 
 private Context mContext; | 
 
005 | 
 private ViewGroup mEmptyOrErrorView; | 
 
006 | 
 private ViewGroup mContentView; | 
 
007 | 
 private ViewGroup mParentView; | 
 
008 | 
 private LayoutInflater mInflater; | 
 
009 | 
 private TextView no_net; | 
 
010 | 
 private Button load_btn; | 
 
011 | 
 private boolean mViewsAdded; | 
 
012 | 
 private ViewGroup.LayoutParams mLayoutParams = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); | 
 
013 | 
 public final static int TYPE_EMPTY = 1; | 
 
014 | 
 public final static int TYPE_ERROR = 2; | 
 
015 | 
 public final static int TYPE_CONTENT = 3; | 
 
016 | 
 private int mType = TYPE_EMPTY; | 
 
019 | 
  * 构造方法,传入上下文及内容GroupView | 
 
021 | 
 public CommonShowView(Context context, ViewGroup mContentView) { | 
 
023 | 
  mInflater = LayoutInflater.from(mContext); | 
 
024 | 
  this.mContentView = mContentView; | 
 
025 | 
  mParentView = (ViewGroup) mContentView.getParent(); | 
 
029 | 
 @SuppressLint("InflateParams") | 
 
030 | 
 private void initViews() { | 
 
031 | 
  mEmptyOrErrorView = (ViewGroup) mInflater.inflate(R.layout.common_show, null); | 
 
032 | 
  no_net = (TextView) mEmptyOrErrorView.findViewById(R.id.no_net); | 
 
033 | 
  load_btn = (Button) mEmptyOrErrorView.findViewById(R.id.load_btn); | 
 
036 | 
   mParentView.addView(mEmptyOrErrorView, mLayoutParams); | 
 
039 | 
  load_btn.setOnClickListener(new OnClickListener() { | 
 
042 | 
   public void onClick(View v) { | 
 
043 | 
    Intent intent = null; | 
 
045 | 
    if (android.os.Build.VERSION.SDK_INT > 10 && !android.os.Build.MANUFACTURER.equals("Meizu")) { | 
 
046 | 
     intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); | 
 
048 | 
    else if (android.os.Build.VERSION.SDK_INT > 17 && android.os.Build.MANUFACTURER.equals("Meizu")) { | 
 
049 | 
     intent = new Intent(android.provider.Settings.ACTION_SETTINGS); | 
 
052 | 
     intent = new Intent(); | 
 
053 | 
     ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); | 
 
054 | 
     intent.setComponent(component); | 
 
055 | 
     intent.setAction("android.intent.action.VIEW"); | 
 
057 | 
    mContext.startActivity(intent); | 
 
063 | 
 public ViewGroup getEmptyOrErrorView() { | 
 
064 | 
  return mEmptyOrErrorView; | 
 
071 | 
 public void setEmptyOrErrorView(ViewGroup emptyOrErrorView) { | 
 
072 | 
  this.mParentView.removeView(this.mEmptyOrErrorView); | 
 
073 | 
  this.mParentView.addView(emptyOrErrorView, mLayoutParams); | 
 
074 | 
  this.mEmptyOrErrorView = emptyOrErrorView; | 
 
081 | 
 public void setEmptyOrErrorView(int res) { | 
 
082 | 
  ViewGroup vg = (ViewGroup) mInflater.inflate(res, null); | 
 
083 | 
  this.mParentView.removeView(this.mEmptyOrErrorView); | 
 
084 | 
  this.mParentView.addView(vg, mLayoutParams); | 
 
085 | 
  this.mEmptyOrErrorView = vg; | 
 
092 | 
 public ViewGroup getContextView() { | 
 
099 | 
 public ViewGroup getRootView() { | 
 
106 | 
 public void setContextView(ViewGroup contentView) { | 
 
107 | 
  this.mType = TYPE_CONTENT; | 
 
114 | 
  * @date 2016-2-19 下午3:02:20 | 
 
116 | 
 public void showByType(int mType) { | 
 
118 | 
  if (mContentView != null) { | 
 
122 | 
     if (mEmptyOrErrorView != null) { | 
 
123 | 
      mEmptyOrErrorView.setVisibility(View.VISIBLE); | 
 
124 | 
      no_net.setText("这里空空也野哦~"); | 
 
125 | 
      load_btn.setVisibility(View.GONE); | 
 
129 | 
     if (mEmptyOrErrorView != null) { | 
 
130 | 
      mEmptyOrErrorView.setVisibility(View.VISIBLE); | 
 
131 | 
      no_net.setText("网络加载失败哦~"); | 
 
132 | 
      load_btn.setVisibility(View.VISIBLE); | 
 
136 | 
     if (mContentView != null) { | 
 
137 | 
      mContentView.setVisibility(View.VISIBLE); | 
 
149 | 
 private void hideAllViews() { | 
 
150 | 
  if (mParentView != null) { | 
 
151 | 
   if (mParentView.getChildCount() > 0) { | 
 
152 | 
    for (int i = 0; i < mParentView.getChildCount(); i++) { | 
 
153 | 
     View childView = mParentView.getChildAt(i); | 
 
154 | 
     if (childView != null) { | 
 
155 | 
      childView.setVisibility(View.GONE); | 
 
 
 
 
02 | 
<?xml version="1.0" encoding="utf-8"?> | 
 
04 | 
 android:id="@+id/rel_null" | 
 
05 | 
 android:layout_width="match_parent" | 
 
06 | 
 android:layout_height="match_parent" > | 
 
09 | 
  android:id="@+id/no_net" | 
 
10 | 
  android:layout_width="match_parent" | 
 
11 | 
  android:layout_height="wrap_content" | 
 
12 | 
  android:layout_centerHorizontal="true" | 
 
13 | 
  android:layout_centerInParent="true" | 
 
14 | 
  android:gravity="center" | 
 
16 | 
  android:textColor="#333333" | 
 
17 | 
  android:textSize="16sp" /> | 
 
20 | 
  android:id="@+id/load_btn" | 
 
21 | 
  android:layout_width="112dp" | 
 
22 | 
  android:layout_height="40dp" | 
 
23 | 
  android:layout_below="@+id/no_net" | 
 
24 | 
  android:layout_centerHorizontal="true" | 
 
25 | 
  android:layout_marginTop="15dp" | 
 
26 | 
  android:background="#E12228" | 
 
27 | 
  android:gravity="center" | 
 
29 | 
  android:textColor="#ffffff" | 
 
30 | 
  android:textSize="14sp" /> | 
 
 
 
 
02 | 
public class MainActivity extends Activity { | 
 
03 | 
 private ListView listview; | 
 
04 | 
 private CommonShowView mShowView; | 
 
05 | 
 private List<String> datas; | 
 
08 | 
 protected void onCreate(Bundle savedInstanceState) { | 
 
09 | 
  super.onCreate(savedInstanceState); | 
 
10 | 
  setContentView(R.layout.activity_main); | 
 
11 | 
  listview = (ListView) findViewById(R.id.listview); | 
 
12 | 
  mShowView = new CommonShowView(this, listview); | 
 
21 | 
  mShowView.showByType(CommonShowView.TYPE_ERROR); | 
 
24 | 
 private List<String> getDatas(boolean flag) { | 
 
25 | 
  List<String> datas = new ArrayList<>(); | 
 
26 | 
  datas.add("这是第一行的data"); | 
 
27 | 
  datas.add("这是第2行的data"); | 
 
28 | 
  datas.add("这是第三行的data"); | 
 
29 | 
  datas.add("这是第4行的data"); | 
 
38 | 
 class MyAdapter extends BaseAdapter { | 
 
39 | 
  private List<String> datas; | 
 
40 | 
  private Context mContext; | 
 
42 | 
  public MyAdapter(Context mContext, List<String> datas) { | 
 
43 | 
   this.mContext = mContext; | 
 
48 | 
  public int getCount() { | 
 
54 | 
  public Object getItem(int position) { | 
 
56 | 
   return datas.get(position); | 
 
60 | 
  public long getItemId(int position) { | 
 
66 | 
  public View getView(int position, View convertView, ViewGroup parent) { | 
 
68 | 
   TextView tv = new TextView(mContext); | 
 
70 | 
   tv.setText(datas.get(position)); | 
 
 
 
 
目前只是做了个超级简单的样式,具体的展示根据需求再改再封装。帖个图:
![]()
![]()
以上就是ListView数据为空及加载错误处理的方法,希望对大家的学习有所帮助。