代码之家  ›  专栏  ›  技术社区  ›  Kanika

Android底稿:我怎么知道用户是在拖拽底部页面呢?

  •  0
  • Kanika  · 技术社区  · 5 年前

    0 回复  |  直到 5 年前
        1
  •  3
  •   Codeversed dimsuz    4 年前

    底层文档不起作用,但我想我想我已经想好了,可以把文档弄清楚:

    隐藏状态为-1.0,折叠状态为0.0,展开状态为1.0。

    只需保留一个名为oldOffSet的属性变量,然后执行以下操作:

    behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
        var oldOffSet = 0f
        override fun onSlide(bottomSheet: View, slideOffset: Float) {
        
            val inRangeExpanding = oldOffSet < slideOffset
            val inRangeCollapsing = oldOffSet > slideOffset
            oldOffSet = slideOffset
        }
    }
    
        2
  •  0
  •   Rohit Soni    5 年前

    final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
            behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState) {
                    if (newState == BottomSheetBehavior.STATE_DRAGGING) {
                        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                    }
                }
    
                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                }
            });
    

    BottomSheetBehavior.STATE_已展开

        3
  •  0
  •   Rupesh Rathore    5 年前

    使用底部工作表行为

    private BottomSheetBehavior sheetBehavior;
    private LinearLayout bottom_sheet;
    bottom_sheet = findViewById(R.id.bottom_sheet);
    sheetBehavior = BottomSheetBehavior.from(bottom_sheet);
    // click event for show-dismiss bottom sheet
    btn_bottom_sheet.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
            sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            btn_bottom_sheet.setText("Close sheet");
        } else {
            sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            btn_bottom_sheet.setText("Expand sheet");
        }
    }
    });
    // callback for do something
    sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View view, int newState) {
        switch (newState) {
            case BottomSheetBehavior.STATE_HIDDEN:
                break;
            case BottomSheetBehavior.STATE_EXPANDED: {
                btn_bottom_sheet.setText("Close Sheet");
            }
            break;
            case BottomSheetBehavior.STATE_COLLAPSED: {
                btn_bottom_sheet.setText("Expand Sheet");
            }
            break;
            case BottomSheetBehavior.STATE_DRAGGING:
                break;
            case BottomSheetBehavior.STATE_SETTLING:
                break;
        }
    }
    
    @Override
    public void onSlide(@NonNull View view, float v) {
    
    }
    });
    
        4
  •  0
  •   Gabriele Mariotti    5 年前

    你可以 BottomSheetCallback 了解现状和方向。

        BottomSheetBehavior<...> bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout);
    
        bottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    
          @Override public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_DRAGGING){
                // Dragging state
            }    
          }
    
          @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            // The new offset of this bottom sheet within [-1,1] range. 
            // Offset increasesas this bottom sheet is moving upward. 
            // From 0 to 1 the sheet is between collapsed and expanded states and 
            // From -1 to 0 it is between hidden and collapsed states.
          }
        });