概述:
在使用SwipeRefreshLayout+ListView进行下拉刷新时,往往需要对获取数据为空的情况进行处理。好在ListView本身提供了一个setEmpty(View view)方法来进行数据为空时的界面处理接口。但是在获取空布局的时候,出现了ListView.setEmpty()不起作用的问题,特此记录一下。
针对数据获取为空的情况,我们可以采取一下两种方式进行添加:
1.在ListView所在的布局直接引入添加空布局
2.使用 LayoutInflater.from(…).inflate(….)或View.inflate(…)方式来添加一个单独的空布局。
今天着重讲解第二种。
第一种方式,我在此处不再细说。直接贴代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".FirstActivity"
android:id="@+id/activity_first"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/empty_view_tab" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
</RelativeLayout>
注意:上述布局中的空布局的引用只能采用此方式,不要妄想在SwipeRefreshLayout中和listView平级的地方引用空布局。因为SwipeRefreshLayout中只能有一个子View.也不要妄想通过下面的方式引用空布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_first"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include layout="@layout/empty_view_tab" />
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
这样虽然满足了SwipeRefreshLayout中只有一个子View的情况,但是还会引起别的问题。详情以及解决方案,可以看下我的另一篇文章解决SwipeRefreshLayout和ListView的EmptyView共存冲突的问题.
今天着重讲一下使用LayoutInflater.from(…).inflate(….)或View.inflate(…)方式来添加一个单独的空布局的方式。
在调用
listView.setEmptyView(getEmptyView());
设置空布局之前,必须要获取空布局的view.但是在获取空布局的时候,出现了ListView.setEmpty()不起作用的问题。主要是由于LayoutInflater使用错误造成的。下面直接贴出两种正确的可以获取空布局view的方式。代码如下:
/**
* @return 获取ListView 的空视图
*/
public View getEmptyView() {
//方式1
View emptyView = LayoutInflater.from(FirstActivity.this).inflate(R.layout.empty_view_tab,
(ViewGroup) swipeRefreshLayout.getParent(), false);
((ViewGroup)swipeRefreshLayout.getParent()).addView(emptyView);
//方式2
// ViewGroup emptyView=(ViewGroup) View.inflate(FirstActivity.this, R.layout.empty_view_tab, null);
// emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// ((ViewGroup)swipeRefreshLayout.getParent()).addView(emptyView);
return emptyView;
}
上述代码已经经过验证,是完全可以的。
代码中需要注意两点:
第一点:
LayoutInflater.from(FirstActivity.this).inflate(xxx,
(ViewGroup) swipeRefreshLayout.getParent(),xxx)
第二个参数不能是listView的父布局(即:swipeRefreshLayout),原因已经说过swipeRefreshLayout中只能有一个子view.如果不这样设置的话,ListView.setEmpty()会不起作用。
第二点:
((ViewGroup)swipeRefreshLayout.getParent()).addView(emptyView);
使用addView()方法时,记得前面的(ViewGroup)swipeRefreshLayout.getParent()要用括号括起来,否则会提示找不到addView()方法。
另外关于LayoutInflater的使用以及参数的用法,给大家推荐一篇文章使用LayoutInflater需要注意的坑
好了,希望上面的备录能够帮你解决你所遇到的问题。