기어가는 피카츄
Swipe Refresh를 할 때 Toolbar가 펼쳐지지 않고 그대로 Swipe만 되는 현상 해결 본문
현재 프로젝트에서는
< fragment_commodity.xml >
CoordinatorLayout
L AppBarLayout
L Toolbar (SwipeRefreshLayout의 움직임에 따라 Collapse/Expanded 감지)
L TabLayout
L TabItem
L SwipeRefreshLayout
L RelativeLayout
L ViewPager (RecyclerView를 띄운다)
L NetworkErrorPage (Include layout) (네트워크 에러 페이지)
레이아웃의 SwipeRefreshLayout 위치에 또 다른 Fragment를 Replace하는 방식으로 구현되어 있다.
Replace되는 Fragment의 Layout은 다음과 같다.
< fragment_commodity_list.xml >
CoordinatorLayout
L NoDataPage (Include layout) (데이터가 없을 때)
L RecyclerView
그런데 문제는 데이터가 없을 때 뜨는 NoDataPage와 NetworkErrorPage에서 Swipe를 했을 때
Toolbar가 expanded된 후 swipe가 동작해야 하는데
Toolbar가 collapsed된 채 그대로 swipe가 동작해버리는 현상이 발생했다.
즉 SwipeRefreshLayout에서 지정한 app:layout_behavior="@string/appbar_scrolling_view_behavior" 인
Layout Behavior이 먹히지 않는 것
생각해 본 해결방안은 두 가지 였음
1. CommodityFragment에서 Tab을 누를 때, NoData인 Tab은(데이터가 없는 ViewPage) 자바단에서 확인 후 자동으로 Toolbar가 pops up 되도록 구현
2. CommodityFragment 에서 SwipeRefreshLayout의 Child인 NetworkErrorPage에 NestedScrollView를 감싸주기
&& 마찬가지로 CommodityListFragment에서 CoorinatorLayout의 Child인 NoDataPage에 NestedScrollView를 감싸주기
간단한 방식은 2번이니까 2번을 채택하기로 했고 성공!
NetworkErrorPage와 NoDataPage는 일반 RelativeLayout이나 ConstraintLayout 등으로 구현한 페이지이기 때문에 Scroll View Behavior이 안 먹는 것은 당연한 문제였던 것 같다.
Scroll View를 일반 layout에 감싸줘야 그제서야 Scroll View Behavior이 먹혔던 것
이제는 데이터를 불러오지 못해도 툴바의 Collapse/Expand 동작이 정상적으로 작동한다.
** 처리 결과물
< fragment_commodity.xml >
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/cl_collaborate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_grey"
android:clickable="true"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:background="@color/white"
android:id="@+id/ab_header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tb_header"
android:background="@color/colorPrimary"
android:theme="@style/AppTheme.Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="0dp"
app:layout_scrollFlags="scroll|enterAlways" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tl_collaborate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabIndicatorColor="@color/colorPrimary">
<com.google.android.material.tabs.TabItem
android:id="@+id/ti_all"
android:text="@string/commodity_tab_all_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.google.android.material.tabs.TabItem>
<com.google.android.material.tabs.TabItem
android:id="@+id/ti_request"
android:text="@string/commodity_tab_collecting_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/srl_commodities"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_grey"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_commodities"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<androidx.core.widget.NestedScrollView
android:background="@color/white"
android:id="@+id/nsv_error"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/rl_error"
layout="@layout/view_error"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_qr_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_qr_code"
app:backgroundTint="@android:color/white" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
< fragment_commodity_list.xml >
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_grey">
<androidx.core.widget.NestedScrollView
android:background="@color/white"
android:id="@+id/nsv_no_data"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/view_no_data"
android:id="@+id/rl_no_data"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.core.widget.NestedScrollView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_commodities"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/view_commodity" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
'아니드로이드' 카테고리의 다른 글
EditText에 focus됐을 때 키보드가 올라오면서 toolbar가 같이 상단으로 올라가는 현상을 막기 위한 방법 (0) | 2019.12.19 |
---|---|
ADB 명령어를 이용하여 Terminal창에서 안드로이드 폰에 APK 파일 심기 (0) | 2019.12.14 |
Release용 앱 배포 시 Google Map SHA-1 key 등록 (지도 띄우는 법) (0) | 2019.12.12 |
EditText 커서 색 변경 (0) | 2019.11.15 |
thyrlian/AwesomeValidation 라이브러리의 문제(?) (0) | 2019.11.12 |