Hello,

kok nae-ga ha-myun an-dweneun MAGIC...🧚

안드로이드

CoordinatorLayout App Bar 드래그 비활성화 + 상단 스크롤 구현

✿도담도담 2018. 5. 1. 01:45


이전글 - [Android] - CoordinatorLayout Behavior 사용기



이전글의 완성된 레이아웃을 참고하면 알겠지만 현재 나는 

CoordinatorLayout에 아래와 같은 뷰들을 두었다.


<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<fragment
android:id="@+id/mapView"
android:name="com.mini_mo.viewpager.Cluster.MapFrg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

여기서 발생한 문제점이 지도 ( map fragment )에서 드래그 시

지도가 움직여야 함에도 불구하고 

CoordinatorLayout 전체가 상하로 움직이는 현상이 발생했다.


이를 해결하기 위해 해당 xml을 가진 activity ( 나는 fragment 지만 ) 자바 파일에서

다음과 같이 코드를 작성해 주었다.

아래쪽에 위로 스크롤 하는 코드와 함께 있다.


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
/* Google Map의 세로 드래그 문제를 해결 하기 위한 부분*/
AppBarLayout appBar = (AppBarLayout) rootView.findViewById(R.id.app_bar);
if (appBar.getLayoutParams() != null) {
    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
    AppBarLayout.Behavior appBarLayoutBehaviour = new AppBarLayout.Behavior();
    appBarLayoutBehaviour.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
        @Override
        public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
            return false;
        }
    });
    layoutParams.setBehavior(appBarLayoutBehaviour);
}
 
/** floating Button 클릭 이벤트 ( 위로 스크롤 하기 ) **/
FloatingActionButton floatingActionButton = (FloatingActionButton) rootView.findViewById(R.id.up_fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Toast.makeText(getContext(), "up", Toast.LENGTH_SHORT).show();
        AppBarLayout appBarLayout = (AppBarLayout) rootView.findViewById(R.id.app_bar);
        NestedScrollView nestedScrollView = (NestedScrollView) rootView.findViewById(R.id.include);
        nestedScrollView.scrollTo(0,0);
        appBarLayout.setExpanded(true);
    }
});
 
cs


AppBar을 객체화 시켜 param ( appbar의 크기 등의 정보 ) 를 가져와 사용하였다.


https://developer.android.com/reference/android/support/design/widget/AppBarLayout.Behavior.DragCallback

( DragCallback 안드로이드 API )


setDragCallback 메소드의 역할을 보니 

AppBar 레이아웃 드래그를 제어 하는 메소드라 설명 돼 있었다.

아마 return false를 해줌으로써 드래그가 비활성화 된 것 같다.

아래 해당 구현 화면이다.

지도의 드래그가 CoordinatorLayout의 영향없이 작동 한다.




위로 스크롤 하는 부분도 몇줄 안되지만 생각보다 힘들었다 😰

구글에 Scrollto, smoothScroll, fullScroll 이었나 여러개의 메소드가 있었는데

움찔 움찔 거리기만 하고 정상 작동이 되지 않았는데

이 역시 AppBar부분 때문이 아닌가 생각이 된다.

appBarLayout.setExpanded(true); 부분과 함께 써주니 해결 되었다.



이 간단한 몇줄 적는것도 얼마나 오래 걸리는지!!

하고나면 항상 별게 아닌 친구들이다

😡😣