programing tip

조각 또는 활동에 도구 모음이있는 코디네이터 레이아웃

itbloger 2020. 9. 2. 14:55
반응형

조각 또는 활동에 도구 모음이있는 코디네이터 레이아웃


새로운 디자인 라이브러리에는 개발자가 원하는 경우 도구 모음의 동작 방식을 많이 변경하는 몇 가지 새로운 레이아웃이 있습니다. 예를 들어 중요한 사진을 보여주는 축소 도구 모음이있는 갤러리 조각 또는 도구 모음을 숨기는 데 앱바 레이아웃이 필요하지 않은 스크롤 뷰가없는 조각과 같이 조각마다 동작과 목표가 다르기 때문에 활동에 단일 도구 모음이 있으면 어렵습니다.

그럼 이것으로 툴바를 각 조각으로 옮겨야할까요? 그렇다면 조각을 표시 할 때마다 supportActionBar를 설정해야하며 조각의 독립적 인 특성을 무효화하는 조각의 활동에 대한 참조도 있어야합니다. 활동에 도구 모음을 그대로두면 각 조각의 각 동작 유형에 대해 여러 레이아웃을 정의해야합니다. 최선의 접근 방법은 무엇입니까?


저에게는 각 조각에 앱 바와 툴바가있는 것이 너무 이상하게 들립니다. 그래서 저는 활동에 툴바가있는 단일 앱 바를 선택했습니다.

CoordinatorLayout으로이 문제를 해결하려면 FrameLayout기본 동작을 재정의하려는 각 조각의 조각을 보유해야하는 다른 동작 (또는 다른 레이아웃)을 설정해야합니다.

기본 동작이 app:layout_behavior="@string/appbar_scrolling_view_behavior"

그런 다음 fragment_activity_layout.xml에 다음과 같은 내용이있을 수 있습니다.

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/dashboard_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.Toolbar"
            app:layout_scrollFlags="scroll|enterAlways"/>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/dashboard_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

그리고 구현하지 않으려는 각 조각에서 다음 동작을 변경하는 메서드 app:layout_behavior="@string/appbar_scrolling_view_behavior"재정의 해야합니다 .onAttachonDetachFrameLayout

CoordinatorLayout.Behavior behavior;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    if(behavior != null)
        return;

    FrameLayout layout =(FrameLayout) getActivity().findViewById(R.id.dashboard_content);
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layout.getLayoutParams();

    behavior = params.getBehavior();
    params.setBehavior(null);

}

@Override
public void onDetach() {
    super.onDetach();
    if(behavior == null)
        return;

    FrameLayout layout =(FrameLayout) getActivity().findViewById(R.id.dashboard_content);
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layout.getLayoutParams();

    params.setBehavior(behavior);

    layout.setLayoutParams(params);

    behavior = null;
}

그 후 CoordinatorLayout은 앱바 등을 축소하지 않으며 조각 레이아웃을 전체 높이로 허용합니다.


내 해결책은 다음과 같습니다.

<!-- Put your fragment inside a Framelayout and set the behavior for this FrameLayout -->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!-- Your fragment -->
    <include layout="@layout/content_main" />

</FrameLayout>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>


이것은 정말 좋은 질문입니다. Toolbar처럼 행동 해야하는는 ActionBara Activity또는 a Fragment? 다양한 질문과 문서를 검색했지만 모든 사례를 다루는 솔루션을 찾을 수 없었습니다. 그러므로 그것은 당신의 상황에 달려 있습니다.

사례 1 : 툴바는 ActionBar를 대체해야합니다.

If the Toolbar has to behave like a normal ActionBar (or if max 1 fragment is shown from time to time), I think the best/simplest way is to use traditional Activities with there own Toolbar and put your Fragment in there. This way you don't have to worry about when which Toolbar must be shown.

Changing the ActionBar (-behaviour) from Fragments is also possible, but I would not recommend it, since that forces you to keep track which Fragment changed the ActionBar when. I don't even know if setting the ActionBar can be done multiple times.

Case 2: Each Fragment should have its own (part of) Toolbar

You could also choose to put different stand alone Toolbars in different Fragments, with their own actions. This way you could display different Fragments next to each other - each with their own actions in their Toolbar - and suggest that it is 1 Toolbar (maybe like the Gmail-app, although I am unsure). This however means that you would have to inflate those Toolbars yourself, but it must not be very difficult.

I hope this will help making a choice.

(Sorry if I made any (language-)mistakes)

참고URL : https://stackoverflow.com/questions/30739806/coordinator-layout-with-toolbar-in-fragments-or-activity

반응형