Dynamically change height of BottomSheetBehavior

后端 未结 9 1122
既然无缘
既然无缘 2020-12-13 03:42

I\'m using the BottomSheetBehavior from Google recently released AppCompat v23.2. The height of my bottom sheet depends on the content displayed inside of the b

9条回答
  •  醉话见心
    2020-12-13 04:15

    I've followed @HaraldUnander advice, and it gave me an idea which has actually worked. If you run a thread (couldn't make it work with the post method as him) after the BottomSheetBehavior.state is set up programmatically to STATE_COLLAPSED, then you can already obtain the height of your views and set the peekHeight depending on it's content.

    So first you set the BottomSheetBehavior:

    BottomSheetBehavior.from(routeCaptionBottomSheet).state = BottomSheetBehavior.STATE_COLLAPSED
    

    And then you set the peekHeight dynamically:

    thread {
        activity?.runOnUiThread {
            val dynamicHeight = yourContainerView.height
            BottomSheetBehavior.from(bottomSheetView).peekHeight = dynamicHeight
        }
    }
    

    If using Java (I'm using Kotlin with Anko for threads), this could do:

    new Thread(new Runnable() {
        public void run() {
            int dynamicHeight = yourContainerView.getHeight();
            BottomSheetBehavior.from(bottomSheetView).setPeekHeight(dynamicHeight);
        }
    }).start();
    

提交回复
热议问题