5

我已经使用appcompat-v7:23.2.1实现了底部工作表。一切正常,但唯一的问题是当我向上拖动布局时,底部工作表出现。我不希望在向上拖动时显示底部工作表当底部页面未显示在屏幕上时,在布局上。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/DarkGray"
tools:context=".bottom_sheets.grid.BottomSheetGridActivity">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <Button
        android:id="@+id/btnView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show view" />
  </LinearLayout>
   <LinearLayout
    android:id="@+id/bottom_sheet1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_marginBottom="8dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:background="#fff"
        />
 </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

主要活动

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bottom_grid);
    Button btnView = (Button) findViewById(R.id.btnView);
    View bottomSheet = findViewById(R.id.bottom_sheet1);
    behavior = BottomSheetBehavior.from(bottomSheet);
    btnView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

             behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });

    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            // React to state change

        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            // React to dragging events
        }
    });

    RecyclerView listRecyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
    listRecyclerView.setHasFixedSize(true);
    listRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    RecyclerItemAdapter recyclerItemAdapter = new RecyclerItemAdapter(createItems(), this);
    listRecyclerView.setAdapter(recyclerItemAdapter);
}

上面的代码将在点击时显示底部工作表,但在布局上拖动时也会显示工作表。我错过了什么。请帮帮我!

4

4 回答 4

6

谢谢@oguzhand

 behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            // React to state change
            if (newState == BottomSheetBehavior.STATE_DRAGGING) {
                behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            // React to dragging events
        }
    });

我现在可以禁用未显示的底部工作表行为。

于 2016-04-02T13:25:52.100 回答
0

You can Set the PeekHeight to 0.

behavior.setPeekHeight(0);
于 2017-04-24T15:12:59.560 回答
-1

在科特林

   view.mtv_title_registration.setOnClickListener {
                if (bottomSheetBehavior.state != BottomSheetBehavior.STATE_EXPANDED){
                    bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
                    val childLayoutParams = view.bottomSheet.layoutParams
                    val displayMetrics = DisplayMetrics()
                    activity?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
                    childLayoutParams.height = displayMetrics.heightPixels
                    view.bottomSheet.layoutParams = childLayoutParams
                }else {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }

            }
于 2019-12-07T23:32:09.827 回答
-1

首先在你的

  onCreate

映射 bottomSheet 后,将 peekheight 设置为 0 并将状态设置为 colpsed

    mBottomSheet.setPeekHeight(0);
    mBottomSheet.setState(BottomSheetBehavior.STATE_COLLAPSED);

这意味着每次活动开始时,它都会默认折叠!然后只需处理回调设置它并在旅途中取消它

       behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        // react to events
          switch(newState){
             case BottomSheetBehavior.STATE_COLLAPSED:
                 mBottomSheet.setPeekHeight(0);
             break;
             case case BottomSheetBehavior.STATE_EXPANDED:
                 mBottomSheet.setPeekHeight(400);
                 //then do your other staff here
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        // React to dragging events
    }
});
于 2017-05-13T06:07:25.207 回答