Android的EmptyView在Fragment中的实现

在App界面里,如果列表中没有元素,界面就会一片空白(列表区域往往是占满大部分屏幕的)。为了避免这种大面积空白的情况,可以在空白的时候显示一行小字,起到提示用户进行下一步操作的作用,也有补位的效果。

Activity中的情况

这种情况比较容易实现,思路是新建一个与RecyclerView同级的TextView元素,将其Visibility属性设为gone(invisible会使得对应位置空白,仍然占用布局空间)。在Activity的回调函数中(参考Activity的生命周期),保证每一次列表更新后,检查列表是否为空,若为空则将RecyclerView的visibility设为gone,TextView的visibility设为visible,此时文字便显现出来。不为空时再改回原样。

参考代码如下:

 
1
private RecyclerView recyclerView;
2
private TextView emptyView;
3
4
// ...
5
6
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
7
emptyView = (TextView) rootView.findViewById(R.id.empty_view);
8
9
// ...
10
11
if (dataset.isEmpty()) {
12
    recyclerView.setVisibility(View.GONE);
13
    emptyView.setVisibility(View.VISIBLE);
14
}
15
else {
16
    recyclerView.setVisibility(View.VISIBLE);
17
    emptyView.setVisibility(View.GONE);
18
}

参考自下列链接:How to show an empty view with a RecyclerView?

Fragment中的情况

然而,现在很多的RecyclerView并不直接是Activity的根元素,尤其是作为Fragment实现时,上述方法不能直接用。参考上面链接中回答者的进一步说明,我实现了相关情况,下面是效果图:

思路其实与上面Activity的情况相同,不同之处在于,检查列表是否为空的操作放在Fragment的回调函数中实现。

代码如下:

 
 
        
1
public class DeadlineFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
2
3
    private SwipeRefreshLayout swipeRefreshLayout;
4
    private View view;
5
6
    public DeadlineList deadlineList;
7
8
    @Nullable
9
    @Override
10
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
11
        view = inflater.inflate(R.layout.fragment_deadline, container, false);
12
        RecyclerView recyclerView = view.findViewById(R.id.deadline_recyclerview);
13
        swipeRefreshLayout = view.findViewById(R.id.deadline_swipe_refresh);
14
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
15
        swipeRefreshLayout.setOnRefreshListener(this);
16
        swipeRefreshLayout.setProgressViewOffset(true, 0, 50);
17
        swipeRefreshLayout.post(new Runnable() {
18
            @Override
19
            public void run() {
20
                swipeRefreshLayout.setRefreshing(true);
21
                onRefresh();
22
            }
23
        });
24
        deadlineList = new DeadlineList();
25
        deadlineList.loadDeadlineList();
26
        setupRecyclerView(recyclerView);
27
        return view;
28
    }
29
30
    @Override
31
    public void onRefresh() {
32
        swipeRefreshLayout.setRefreshing(false);
33
        RecyclerView recyclerView = view.findViewById(R.id.deadline_recyclerview);
34
        TextView textView = view.findViewById(R.id.deadline_empty_text);
35
        if (deadlineList.isEmpty()) {
36
            recyclerView.setVisibility(GONE);
37
            textView.setVisibility(View.VISIBLE);
38
        } else {
39
            recyclerView.setVisibility(View.VISIBLE);
40
            textView.setVisibility(GONE);
41
        }
42
    }
43
}

由于这里的RecyclerView放在了SwipeRefreshLayout中,因此Fragment实现了对应的OnRefreshListener,也有了对应函数OnRefresh,就把检查列表是否为空的操作放进了OnRefresh里(程序启动的时候会自动调用一次的,不需要手动调用)。

另外不能直接把RecyclerView和TextView并排放在SwipeRefreshLayout中,会出现问题,我的做法是用一个LinearLayout把它们两个包起来再放进去,xml代码如下:

 
 
        
1
<?xml version="1.0" encoding="utf-8"?>
2
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    android:id="@+id/deadline_swipe_refresh"
4
    android:layout_width="match_parent"
5
    android:layout_height="match_parent">
6
    <LinearLayout
7
        android:paddingTop="@dimen/activity_vertical_margin"
8
        android:paddingBottom="@dimen/activity_vertical_margin"
9
        android:paddingLeft="@dimen/activity_horizontal_margin"
10
        android:paddingRight="@dimen/activity_horizontal_margin"
11
        android:layout_width="match_parent"
12
        android:layout_height="match_parent"
13
        android:orientation="vertical">
14
        <android.support.v7.widget.RecyclerView
15
            android:id="@+id/deadline_recyclerview"
16
            android:layout_width="match_parent"
17
            android:layout_height="match_parent"
18
            android:visibility="gone"/>
19
        <TextView
20
            android:id="@+id/deadline_empty_text"
21
            android:gravity="center_horizontal"
22
            android:layout_width="match_parent"
23
            android:layout_height="wrap_content"
24
            android:text="@string/deadline_empty_text"
25
            android:visibility="visible" />
26
    </LinearLayout>
27
28
</android.support.v4.widget.SwipeRefreshLayout>

 

猜你喜欢

转载自blog.csdn.net/lgczym/article/details/80003169