我看到很多教程和其他内容在列表为空时设置空视图。我有一个能够刷卡刷新的列表。现在,我想在列表视图为空时在文本视图中显示文本消息。我已经知道它可以很简单地设置这个
myListview.setEmptyView(mEmptyViewContainer);
但是当listview中的数据为null时,它不会设置textview。以下是读取问题后修改我的xml,他通过将文本视图包装在单独的swipeRefreshLayout中来解决。请看下面,这是我的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"
android:background="#dadada"
>
<RelativeLayout
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeRefreshLayout_emptyView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sdasdadadfdasf"
android:layout_centerInParent="true"
android:visibility="gone"
android:gravity="center"
/></ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
所以现在我不明白为什么它没有显示我的空视图?可能是什么问题?或者请告诉我是否有其他方法可以做到这一点?该代码片段将不胜感激。
答案 0 :(得分:0)
你可以这样做:
你的xml中的:
<RelativeLayout>
<SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout">
</SwipeRefreshLayout>
<TextView
android:id="@+id/layout_empty"/>
</RelativeLayout>
java代码:
swipeView = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_refresh_layout);
layoutEmpty = getActivity().findViewById(R.id.layout_empty);
列表视图为空时:
layoutEmpty.setVisibility(View.VISIBLE);
当列表视图不为空时:
layoutEmpty.setVisibility(View.INVISIBLE);