Android

리사이클러뷰 리스트 비었을때 EmptyView 보여주기

그란. 2017. 11. 11. 19:32

RecyclerView 에서 아이템이 0이 되는 순간 emptyView(TextView)를 보여준다 (옵저버 패턴인듯)




1. RecyclerView 대신 RecyclerViewEmptySupport Custom Class 를 사용


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class RecyclerViewEmptySupport extends RecyclerView {
    private View emptyView;
 
    private AdapterDataObserver emptyObserver = new AdapterDataObserver() {
 
        @Override
        public void onChanged() {
            Adapter<?> adapter = getAdapter();
            if (adapter != null && emptyView != null) {
                if (adapter.getItemCount() == 0) {
                    emptyView.setVisibility(View.VISIBLE);
                    RecyclerViewEmptySupport.this.setVisibility(View.GONE);
                } else {
                    emptyView.setVisibility(View.GONE);
                    RecyclerViewEmptySupport.this.setVisibility(View.VISIBLE);
                }
            }
 
        }
    };
 
    public RecyclerViewEmptySupport(Context context) {
        super(context);
    }
 
    public RecyclerViewEmptySupport(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public RecyclerViewEmptySupport(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
 
    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);
 
        if (adapter != null) {
            adapter.registerAdapterDataObserver(emptyObserver);
        }
 
        emptyObserver.onChanged();
    }
 
    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
    }
}
cs






2. Layout 설정


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="20dp"
    >
 
    <com.example.a0b.move2dinerforuser.RecyclerViewEmptySupport
        android:layout_margin="10dp"
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 
    <TextView
        android:id="@+id/empty_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="평가한 트럭이 없습니다"
        android:textSize="25sp" />
 
</RelativeLayout>
cs




3. MainActivity



1
2
3
4
5
6
7
8
9
10
   //전역변수
 private RecyclerViewEmptySupport recyclerview;
    private Adapter favAdapter;
 
 
//onCreate() 
    recyclerview = (RecyclerViewEmptySupport) findViewById(R.id.recyclerview);
    recyclerview.setLayoutManager(new LayoutManager(this));
    favAdapter = new Adapter(list, this);
    recyclerview.setAdapter(adapter);
    recyclerview.setEmptyView(findViewById(R.id.emptyview));
cs