问题描述
我正在开发一个使用 GoogleMap (v2) 和 Recyclerview(以及许多其他视图)的项目。当应用程序处于纵向模式时,(在顶部显示地图,在下面显示回收站视图),我希望它可以“拖动”回收站视图和地图之间的交点,因此地图可以更高/更低(以及recyclerview 的高度会相应调整)。
原则上我的布局是这样的:
<?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.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/fragmentBackgroundColor">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- other views in same level here,not essentially for the Q-->
<com.google.android.gms.maps.MapView
android:id="@+id/fragment_home_map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:liteMode="false"
app:mapType="hybrid" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<include
android:id="@+id/fragment_home_info_card_car_position"
layout="@layout/info_card_car_position"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
tools:visibility="visible"
android:visibility="visible" />
<include
android:id="@+id/fragment_home_filter_chips"
layout="@layout/selection_chips"
app:layout_constraintTop_toBottomOf="@id/fragment_home_info_card_car_position"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:background="?android:attr/listDivider"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/fragment_home_swiper_refresh_layout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/fragment_home_divider_2"
android:layout_width="match_parent"
android:layout_height="0dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/fragment_home_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadeScrollbars="false"
android:scrollbars="vertical"
android:scrollbarSize="4dp"
android:scrollbarStyle="outsideOverlay"
app:layoutManager="androidx.recyclerview.widget.linearlayoutmanager"
tools:listitem="@layout/list_item_order_list"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<View
app:layout_constraintBottom_toTopOf="@id/fragment_home_bottom_navigation"
android:layout_marginBottom="4dp"
android:id="@+id/fragment_home_divider_2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/fragment_home_bottom_navigation"
style="@style/Widget.MaterialComponents.BottomNavigationView.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation_view_menu"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
在地图和回收器视图之间,有两个可用作拖动处理程序的视图(触摸并向上/向下拖动它们)。
我希望(并且还没有找到)某种手势处理程序,我可以环绕这两个处理程序,使它们可以向上/向下拖动并调整地图和回收站视图的大小,而让另一个保持原样(并让如果需要,他们会进行调整)。
或者你有什么好的建议。
解决方法
我会为此建议 BottomSheetBehaviour
。只需在继承 BottomSheetBehavior 的 Layout 中包含 Handle + Layout,就会像魅力一样工作。为此,您只需对现有代码进行很少的更改,这就是我更喜欢这种方式的原因。
其次,如果您想通过对手柄的高级控制进行高级拖动/滑动,滑动时具有扩展/收缩视图的滑动,请使用 MotionLayout
。搜索这两个,你会找到很多答案,或者我会帮助更多。