在项目中,都会用到ListView或GridView等列表控件。一般会用来展示从网络请求的数据 。如果请求的数据为空或者在请求的时候正好无没有网络了,我们的界面应该如何展示呢?数据为空的时候,ListView可以使用setEmptyView (View emptyView) 方法来我们需要的统一界面。数据加载失败呢?我们也可以统一进行处理。
//下面这个类是简单地封装用于无数据及加载错误的一个页面。
public class CommonShowView {
private Context mContext;// 上下文
private ViewGroup mEmptyOrErrorView;// 页面加载无数据或加载错误时显示
private ViewGroup mContentView;// 加载成功时显示的内容
private ViewGroup mParentView;// 父布局viewGroup
private LayoutInflater mInflater;
private TextView no_net;
private Button load_btn;
private boolean mViewsAdded;
private ViewGroup.LayoutParams mLayoutParams = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
public final static int TYPE_EMPTY = 1;// 数据为空
public final static int TYPE_ERROR = 2;// 加载数据失败
public final static int TYPE_CONTENT = 3;// 直接显示内容
private int mType = TYPE_EMPTY;// 数据类型,默认是无数据
public CommonShowView(Context context, ViewGroup mContentView) {
mContext = context;
mInflater = LayoutInflater.from(mContext);
this.mContentView = mContentView;
mParentView = (ViewGroup) mContentView.getParent();
initViews();
}
@SuppressLint("InflateParams")
private void initViews() {
mEmptyOrErrorView = (ViewGroup) mInflater.inflate(R.layout.common_show, null);
no_net = (TextView) mEmptyOrErrorView.findViewById(R.id.no_net);
load_btn = (Button) mEmptyOrErrorView.findViewById(R.id.load_btn);
if (!mViewsAdded) {
mViewsAdded = true;
mParentView.addView(mEmptyOrErrorView, mLayoutParams);
}
load_btn.setOnClickListener(new OnClickListener() {// 检查网络,进行网络设置
@Override
public void onClick(View v) {
Intent intent = null;
// 判断手机系统的版本 即API大于10 就是3.0或以上版本及魅族手机
if (android.os.Build.VERSION.SDK_INT > 10 && !android.os.Build.MANUFACTURER.equals("Meizu")) {
intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}
else if (android.os.Build.VERSION.SDK_INT > 17 && android.os.Build.MANUFACTURER.equals("Meizu")) {
intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
}
else {
intent = new Intent();
ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
mContext.startActivity(intent);
}
});
}
public ViewGroup getEmptyOrErrorView() {
return mEmptyOrErrorView;
}
public void setEmptyOrErrorView(ViewGroup emptyOrErrorView) {
this.mParentView.removeView(this.mEmptyOrErrorView);
this.mParentView.addView(emptyOrErrorView, mLayoutParams);
this.mEmptyOrErrorView = emptyOrErrorView;
}
public void setEmptyOrErrorView(int res) {
ViewGroup vg = (ViewGroup) mInflater.inflate(res, null);
this.mParentView.removeView(this.mEmptyOrErrorView);
this.mParentView.addView(vg, mLayoutParams);
this.mEmptyOrErrorView = vg;
}
public ViewGroup getContextView() {
return mContentView;
}
public ViewGroup getRootView() {
return mParentView;
}
public void setContextView(ViewGroup contentView) {
this.mType = TYPE_CONTENT;
showByType(mType);
}
public void showByType(int mType) {
hideAllViews();
if (mContentView != null) {
switch (mType) {
case TYPE_EMPTY:
if (mEmptyOrErrorView != null) {
mEmptyOrErrorView.setVisibility(View.VISIBLE);
no_net.setText("这里空空也野哦~");
load_btn.setVisibility(View.GONE);
}
break;
case TYPE_ERROR:
if (mEmptyOrErrorView != null) {
mEmptyOrErrorView.setVisibility(View.VISIBLE);
no_net.setText("网络加载失败哦~");
load_btn.setVisibility(View.VISIBLE);
}
break;
case TYPE_CONTENT:
if (mContentView != null) {
mContentView.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
}
}
private void hideAllViews() {
if (mParentView != null) {
if (mParentView.getChildCount() > 0) {
for (int i = 0; i < mParentView.getChildCount(); i++) {
View childView = mParentView.getChildAt(i);
if (childView != null) {
childView.setVisibility(View.GONE);
}
}
}
}
}
}
//错误页面及空页面时布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rel_null"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/no_net"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:gravity="center"
android:text="好像没网哦~"
android:textColor="#333333"
android:textSize="16sp" />
<Button
android:id="@+id/load_btn"
android:layout_width="112dp"
android:layout_height="40dp"
android:layout_below="@+id/no_net"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:background="#E12228"
android:gravity="center"
android:text="检查网络"
android:textColor="#ffffff"
android:textSize="14sp" />
</RelativeLayout>
//在Activity中使用
public class MainActivity extends Activity {
private ListView listview;
private CommonShowView mShowView;
private List<String> datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
mShowView = new CommonShowView(this, listview);
// ----------有数据的时情况下--------------
// datas = getDatas(true);
// listview.setAdapter(new MyAdapter(this, datas));
// mShowView.setContextView(listview);//显示数据,也可以不调用,直接显示
// --------无数据的情况下----------------
// datas = getDatas(false);//无数据
// mShowView.showByType(CommonShowView.TYPE_EMPTY);
// ---------数据加载失败的情况下---------------
mShowView.showByType(CommonShowView.TYPE_ERROR);
}
private List<String> getDatas(boolean flag) {
List<String> datas = new ArrayList<>();
datas.add("这是第一行的data");
datas.add("这是第2行的data");
datas.add("这是第三行的data");
datas.add("这是第4行的data");
if (flag) {
return datas;
}
else {
return null;
}
}
class MyAdapter extends BaseAdapter {
private List<String> datas;
private Context mContext;
public MyAdapter(Context mContext, List<String> datas) {
this.mContext = mContext;
this.datas = datas;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 这里只举个小例子,没有考虑性能 优化
TextView tv = new TextView(mContext);
tv.setTextSize(16f);
tv.setText(datas.get(position));
return tv;
}
}
}
目前只是做了个超级简单的样式,具体的展示根据需求再改再封装。帖个图:
以上就是ListView数据为空及加载错误处理的方法,希望对大家的学习有所帮助。
您可能感兴趣的文章:java 查找list中重复数据实例详解Android操作SQLite数据库(增、删、改、查、分页等)及ListView显示数据的方法详解Android中ListView结合CheckBox实现数据批量选择(全选、反选、全不选)Android编程使用ListView实现数据列表显示的方法Android实现ListView分页自动加载数据的方法Android List删除重复数据