【问题标题】:How to collapse a view to the bottom of the screen in android using coordinator layout?如何使用协调器布局在android中将视图折叠到屏幕底部?
【发布时间】:2018-12-05 10:52:07
【问题描述】:

谁能帮我让一个可折叠工具栏在不退出屏幕的情况下转到底部?

初始视图:

向下滚动后:

这是我尝试过的,但工具栏总是滚动到顶部并消失:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:id="@+id/search_place_root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.location.SearchPlaceActivity">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/search_place_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/home_background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <EditText
            android:id="@+id/search_place_et"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="25dp"
            android:layout_marginStart="57dp"
            android:layout_marginTop="8dp"
            android:background="#e3e3e3"
            android:padding="10dp"
            android:textColor="@color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/search_place_back_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="25dp"
            app:layout_constraintBottom_toBottomOf="@+id/search_place_et"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/search_place_et"
            app:srcCompat="@drawable/back" />
    </android.support.constraint.ConstraintLayout>


    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/search_place_header"
        android:foregroundGravity="bottom">


        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">


            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/search_place_results_container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/list_search"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:background="@color/white"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />


                    <android.support.constraint.ConstraintLayout
                        android:layout_width="0dp"
                        android:layout_height="122dp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/list_search">

                        <ImageView
                            android:id="@+id/imageView22"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="25dp"
                            android:src="@drawable/location_pin"
                            app:layout_constraintBottom_toBottomOf="@id/search_place_use_my_current_loc_tv"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="@id/search_place_use_my_current_loc_tv" />

                        <TextView
                            android:id="@+id/search_place_use_my_current_loc_tv"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="22dp"
                            android:layout_marginTop="34dp"
                            android:fontFamily="@font/opensanssemibold"
                            android:letterSpacing="0.02"
                            android:lineSpacingExtra="12sp"
                            android:text="@string/use_my_current_location"
                            android:textColor="#000000"
                            android:textSize="15sp"
                            app:layout_constraintStart_toEndOf="@+id/imageView22"
                            app:layout_constraintTop_toTopOf="parent" />
                    </android.support.constraint.ConstraintLayout>
                </android.support.constraint.ConstraintLayout>
            </android.support.design.widget.CollapsingToolbarLayout>

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


</android.support.constraint.ConstraintLayout>

如何使我的整个内容过渡到底部并缩小一点?

【问题讨论】:

    标签: java android scroll material-design android-coordinatorlayout


    【解决方案1】:

    Xml

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="info.androidhive.bottomsheet.MainActivity"
        tools:showIn="@layout/activity_main">
    
        <-Your Views->
    
     </LinearLayout>
    

    Java

    BottomSheetBehavior sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet);
    
    /**
             * bottom sheet state change listener
             * we are changing button text when sheet changed state
             * */
            sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState) {
                    switch (newState) {
                        case BottomSheetBehavior.STATE_HIDDEN:
                            break;
                        case BottomSheetBehavior.STATE_EXPANDED: {
                            btnBottomSheet.setText("Close Sheet");
                        }
                        break;
                        case BottomSheetBehavior.STATE_COLLAPSED: {
                            btnBottomSheet.setText("Expand Sheet");
                        }
                        break;
                        case BottomSheetBehavior.STATE_DRAGGING:
                            break;
                        case BottomSheetBehavior.STATE_SETTLING:
                            break;
                    }
                }
    
                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {
    
                }
            });
    

    See more details

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多