首页 > 解决方案 > 有没有办法制作视图拖动手柄

问题描述

我正在开发一个使用 GoogleMap (v2) 和 Recyclerview(以及许多其他视图)的项目。当应用程序处于纵向模式时(在顶部显示地图,在下方显示 recyclerview),我希望能够“拖动”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>

显示我想要拖动手柄的位置

在地图和回收视图之间,有两个包含的视图可以用作拖动处理程序(触摸并向上/向下拖动它们)。

我希望(并且还没有找到)某种手势处理程序我可以环绕这两个以使它们可以向上/向下拖动并调整地图和回收器视图的大小,并保持另一个不变(并让他们调整如果需要)。

或者你还有什么好的建议。

标签: androidkotlingoogle-maps-android-api-2draggable

解决方案


我会BottomSheetBehaviour为此提出建议。只需将 Handle + Layout 包含在继承 BottomSheetBehavior 的 Layout 中,就会像魅力一样工作。为此,您需要对现有代码进行很少的更改,这就是我喜欢这种方式的原因。

其次,如果您想通过对手柄的高级控制来进行高级拖动/滑动,在滑动时使用扩展/收缩视图进行滑动,请使用MotionLayout. 搜索这两个,你会找到很多答案,或者我会帮助更多。


推荐阅读