BottomSheetBehaviour的可调高度

时间:2016-12-08 13:36:56

标签: android android-linearlayout share bottom-sheet

我正在使用BottomSheetBehaviour进行共享功能。

我正在尝试实现以下功能

  • 假设我有20个可以分享的应用程序。所以我需要在底部显示20个应用程序作为项目(这将是一个可扩展的列表视图)。
  • 最初按下Share按钮时,我想将BottomSheet高度修改为screenHeight/2
  • 当用户向上拖动BottomSheet时,我想增加BottomSheet的高度,类似于WrapContent

BottomSheet因为我是新手而非常混乱。任何人都可以指导我或提供任何参考。

代码:

的xml:

<LinearLayout
        android:layout_gravity="center"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        app:behavior_hideable="true"
        app:behavior_peekHeight="0dp"
        android:layout_marginBottom="20dp"
        android:id="@+id/bottom_sheet"
        android:background="@android:color/white"
     app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="25dp"
        android:textStyle="bold"
        android:text="Share via..."/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:id="@+id/listview"/>

</LinearLayout>

的java:

View bottom_sheet;
BottomSheetBehavior bSheet
ListView listview;`
bottom_sheet=getActivity().findViewById(R.id.bottom_sheet);
bSheet= BottomSheetBehavior.from(bottom_sheet);
listView= (ListView) getActivity().findViewById(R.id.listview);

分享代码:

public void shareKnapp() {
        bSheet.setHideable(true);
        if(bSheet.getState()==BottomSheetBehavior.STATE_EXPANDED){
            bSheet.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }else{
        bSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Share funtionality
           });
    }

1 个答案:

答案 0 :(得分:0)

如果您希望BottomSheetBehaviour恰好是屏幕的一半,可以先获取显示尺寸,然后按如下所示进行设置:

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int screenHeight = metrics.heightPixels;

bSheet.setPeekHeight(screenHeight);

您现在可以将默认状态设置为STATE_HIDDEN,并通过单击bSheet.setState(BottomSheetBehavior.STATE_COLLAPSED);在共享按钮上显示它。

现在,如果底部有一个ListView,则当用户拖动它时它应该自动滚动。

另外,我会考虑看看BottomSheetDialogFragment,因为它几乎可以满足您的需求。如果屏幕上有一个需要从底部滚动的固定部分(并且并非一直隐藏),则BottomSheetBehaviour很有用。

相关问题