如何创建此布局?

时间:2018-05-20 08:16:52

标签: android android-layout

我正在处理需要布局的应用程序,如下所述。

enter image description here

布局中有四个部分,我们说布局A,B,C和D.

  1. 布局A 是AppBar并固定在最顶层。
  2. 布局D 已隐藏,并在向上滚动时显示一些其他效果。

    • 向上滚动 Layout-D 从底部出现,而 Layout-C 开始淡出。
    • 一切都保持原样,直到 Layout-D 完全重叠 Layout-C
    • 布局-D 布局-C 完全重叠后,进一步向上滚动布局B 开始向上滚动。
  3. 同样在向下滚动时,首先 Layout-D 向下滚动,然后会出现 Layout-B 。进一步向下滚动布局-D 会隐藏,布局-C 会再次显示淡出效果。

  4. 我已经花了两天时间尝试使用带有协调器布局的BottomSheet,但无法实现精确效果。此外,BottomSheet滚动效果并不平滑,即如果出现并突然隐藏。

    我想要使用的另一件事是具有协调器布局的自定义行为,但不确定如何继续。

    check

1 个答案:

答案 0 :(得分:1)

从父活动或片段中使用具有底部工作表行为的片段:

<android.support.design.widget.CoordinatorLayout
        android:id="@+id/bottomsheetfragmenCoordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBlackOpacity"
        android:clickable="true"
        >
        <android.support.constraint.ConstraintLayout
            android:id="@+id/bottomsheetfragmentLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:behavior_hideable="true"
            app:behavior_peekHeight="50dp"
            app:elevation="4dp"
            app:layout_behavior="@string/bottom_sheet_behavior">

            <fragment
                android:id="@+id/bottomsheetfragment"
                android:name="com.example.inslem.it.BottomSheet"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent" />
       </android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>

底片片段:

public class BottomSheet extends Fragment {

    public BottomSheet() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_bottom_sheet, container, false);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
}

并从父活动或父片段中获取 得到底页

@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    bottomsheetfragmenCoordinatorLayout = view.findViewById(R.id.bottomsheetfragmenCoordinatorLayout);
            behavior = BottomSheetBehavior.from(bottomsheetfragmentLayout);
            behavior.setPeekHeight(250);
            behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState) {
                    switch (newState) {
                        case BottomSheetBehavior.STATE_DRAGGING:
                            break;
                        case BottomSheetBehavior.STATE_SETTLING:
                            break;
                        case BottomSheetBehavior.STATE_EXPANDED:
                            break;
                        case BottomSheetBehavior.STATE_COLLAPSED:
                            break;
                        case BottomSheetBehavior.STATE_HIDDEN:
                            break;
                    }
                }

                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                    Log.i("BottomSheetCallback", "slideOffset: " + slideOffset);
                }
            });
}
相关问题