我正在使用ListView
并在用SwipeRefresh
向下滑动时刷新它,直到这里工作正常。当我尝试设置TextView
时出现问题,mListview.setEmptyView()
滑动时未显示Refresh Icon
。
看起来它在TextView
下.但是,如果我不将该TextView
放在同一个XML上,它可以正常工作,但是当列表为空时,我需要显示一条消息。是什么导致了这种行为?
这是我使用的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/padding_light"
android:paddingRight="@dimen/padding_light">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ListView
android:id="@+id/mListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<TextView
android:id="@+id/swipeToRefreshTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:gravity="center_horizontal"
android:text="@string/msg_swipe_to_refresh"
android:visibility="gone" />
</RelativeLayout>
下面是刷新侦听器:
//Listeners
mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//Here I do some job
beaconScanSwipeRefresh.setRefreshing(false);
}
});
这是我设置ListView
和空视图适配器的地方:
//UI
mListLv = (ListView) view.findViewById(R.id.mListView);
mSwipeRefresh=(SwipeRefreshLayout)view.findViewById(R.id.swipeRefreshLayout);
swipeToRefreshTv = (TextView)view.findViewById(R.id.swipeToRefreshTextView);
//Set Adapter
mListLv.setAdapter(mBaseAdapter);
//Set Message when list is empty
mListLv.setEmptyView(swipeToRefreshTv);
我试图将TextView
移动到<android.support.v4.widget.SwipeRefreshLayout
标签内或标签上方。结果还是一样。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/padding_light"
android:paddingRight="@dimen/padding_light">
<TextView
android:id="@+id/swipeToRefreshTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/msg_swipe_to_refresh"
android:visibility="gone" />
<android.support.v4.widget.SwipeRefreshLayout
//below = swipeToRefreshTextView
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ListView
android:id="@+id/mListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
请以这种方式更新您的布局并检查。
你的SwipeRefreshLayout
和你的TextView
在同一RelativeLayout
,其中一个就会在彼此之上。
我会尝试将其与您的ListView
放在同一水平,然后它们都放在一个容器内,在SwipeRefreshLayout
内.而且由于SwipeRefreshLayout
是布局中的单个项目,因此您可以删除外部RelativeLayout
。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/padding_light"
android:paddingRight="@dimen/padding_light">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/mListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/swipeToRefreshTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:gravity="center_horizontal"
android:text="@string/msg_swipe_to_refresh"
android:visibility="gone" />
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
您可以尝试将ListView
或RecyclerView
放入ScrollView
中。对我来说,它做到了。
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/mListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>