ListView使用EmptyView的必要條件
listView.setEmptyView(view)
方法中的view必須和listView屬於同一個ViewGroup。負責即使添加了也不會起作用。
SwipeRefreshLayout下添加EmptyView,不能實現效果的原因
SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout)
雖然是ViewGroup的子類,但是他只允許有一個子控件,所以在它下面同時添加ListView和EmptyView,EmptyView不能註冊到SwipeRefreshLayout。
private View mTarget; // the target of the gesture
...
private void ensureTarget() {
// Don't bother getting the parent height if the parent hasn't been laid
// out yet.
if (mTarget == null) {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (!child.equals(mCircleView)) {
mTarget = child;
break;
}
}
}
}
SwipeRefreshLayout中添加FramLayout包含ListView和EmptyView存在的問題
這個方案能夠在ListView爲空的時候顯示EmptyView,但是影響ListView的下拉(與下拉刷新空間衝突)。
通過代碼添加EmptyView的問題
通過
listView.getParent()
可以獲取到ListView的父控件,通過它的addView()
可以添加EmptyView。但是當列表爲空的時候就不能再下拉刷新了。
完善的解決方法
通過ViewGroup(不能是LinearLayout)下添加兩個SwipeRefreshLayout,一個包含ListView,另外一個包含被ScrollView包含的EmptyView。第二個SwipeRefreshLayout作爲EmptyView,需要ScrollView是因爲需要他來接受下拉事件傳遞。
代碼中,兩個SwipeRefreshLayout最好是設置一樣的樣式和下拉監聽。
佈局文件和代碼如下:
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tobrun.example.swipetorefresh.MainActivity">
<ImageView
android:id="@+id/igHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/igProgress">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout_emptyView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/igProgress">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/emptyView"
android:text="@string/empty"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center" />
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
activity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
……此處省去findViewById()以及部分初始化
initSwipeLayout(swipeLayout);
initSwipeLayout(swipeLayoutEmpty);
lv.setEmptyView(swipeLayoutEmpty);
lv.setAdapter(adapter);
}
private void initSwipeLayout(SwipeRefreshLayout layout) {
layout.setColorSchemeResources(R.color.ics_blue_bright, R.color.ics_green_light,
R.color.ics_orange_light, R.color.ics_red_light);
layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
reloadData();
}
});
}
//此處使用的是AndroidAnnotations,意義就是使用了非UI線程
@Background
void reloadData() {
// getData methods
......
bindData();
}
//此處使用的是AndroidAnnotations,意義就是使用了UI線程,同runOnUIThread
@UIThread
void bindData() {
// bindDataMethods
.......
swipeLayout.setRefreshing(false);
swipeLayoutEmpty.setRefreshing(false);
}