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 |
'Android' 카테고리의 다른 글
CustomTitlebar 재활용 (0) | 2017.11.20 |
---|---|
슬라이더 무한 + 자동 스크롤 + Indicator 연동 (ViewPager) (0) | 2017.11.20 |
Easy Way] TedPermission + 현재 위치 -> 구글맵 (0) | 2017.11.11 |
움직이는 로딩 이미지 AppCompatDiaLog + Lotti (1) | 2017.11.09 |
횡스크롤 안되는 FragmentViewPager + 하단 네비게이션 (1) | 2017.11.09 |