Method setHalfExpandedRatio(float ratio)

爷,独闯天下 提交于 2020-05-17 06:44:13

问题


It's my first question in this incredible community.

I'm developing an android app in kotlin. I need a permanent bottomsheet (not modal). I have developed all the behavior that I needed, but for one detail.

I need to set de STATE_HALF_EXPANDED, by default is 50% of the screen, but I need 70%. I have visited this question: How i can set Half Expanded state for my BottomSheet

In that question, the user Adauton Heringer explain how to do it in one of the answers. He said:

behavior = BottomSheetBehavior.from(your_bottom_sheet_xml)
behavior.isFitToContents = false
behavior.halfExpandedRatio = 0.6f

I tried the same, because it looks like very easy to do. I did the two first lines, but when I try to used setHalfExpandedRatio() is like it wasn't exist. I have checked the official documentation and it is a public method.

https://developer.android.com/reference/com/google/android/material/bottomsheet/BottomSheetBehavior#sethalfexpandedratio

Am I doing something wrong?

My code is this:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        var bottomSheet: View = view.findViewById(R.id.departures_bottomsheet)
        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
        bottomSheetBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
        bottomSheetBehavior.isFitToContents = false

        // this doesn't work for me
        // bottomSheetBehavior.halfExpandedRatio = 0.7 

        bottomSheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onSlide(p0: View, dragPoint: Float) {
                val upper = 0.66
                val lower = 0.33
                if (dragPoint >= upper) {
                    bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
                }
                if (dragPoint < upper && dragPoint >= lower) {
                    bottomSheetBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
                }
                if (dragPoint < lower) {
                    bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
                }
            }

            override fun onStateChanged(p0: View, p1: Int) {

            }
        } )
    }

I have this import: import com.google.android.material.bottomsheet.BottomSheetBehavior

And this implementation in build.gradle of the app: implementation 'com.google.android.material:material:1.0.0'

In the layout file, the View is a child of a CoordinatorLayout

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/globalMap"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <com.google.android.material.circularreveal.CircularRevealLinearLayout
            android:id="@+id/departures_bottomsheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            app:behavior_peekHeight="80dp"
            app:layout_behavior="@string/bottom_sheet_behavior">

I have navigated to BottomSheetBehavior.class in Android Studio and this method doesn't exist.

Any help is welcome and I will be grateful.

If I don't find any other way, I will create a SubClass with this method.


回答1:


From BottomSheetBehavior.java commit history, the method setHalfExpandedRatio(float ratio) is added from version 1.1.0-alpha05.

You are using vevrsion 1.0.0, that why you cannot see this method.

Solution: Change version code from 1.0.0 to 1.1.0-alpha05 in your gradle file.

// implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha05'

or using the latest version

// implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'

You can find all available versions here.



来源:https://stackoverflow.com/questions/59424273/method-sethalfexpandedratiofloat-ratio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!