7

从版本23.4.0切换到Android设计支持库的版本24.2.1后,BottomSheetBehavior停止工作。底部表格显示为打开并在致电setState(BottomSheetBehavior.STATE_COLLAPSED)时不关闭。这不会发生在BottomSheetBehaviour按预期工作的设计库的23.4.0版本中。Android设计支持库24.2.1使BottomSheet在启动时打开

版本24中是否有任何更改需要使用BottomSheetBehavior的不同?

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/button" 
     android:text="Open Bottom Sheet" 
     /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/close_button" 
     android:text="Close Bottom Sheet" 
     /> 

</LinearLayout> 
<LinearLayout 
    android:id="@+id/bottom_sheet" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:orientation="horizontal" 
    android:background="@android:color/holo_green_light" 
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"/> 

这里是我使用的活动代码:

public class ScrollingActivity extends AppCompatActivity implements View.OnClickListener { 

private View m_bottomSheet; 
private BottomSheetBehavior m_behaviour; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_scrolling); 

    m_bottomSheet = findViewById(R.id.bottom_sheet); 
    m_behaviour = BottomSheetBehavior.from(m_bottomSheet); 


    ((Button)findViewById(R.id.button)).setOnClickListener(this); 
    ((Button)findViewById(R.id.close_button)).setOnClickListener(this); 
} 
@Override 
public void onClick(View v) { 
    switch(v.getId()){ 
     case R.id.button: 
      m_behaviour.setState(BottomSheetBehavior.STATE_EXPANDED); 
      break; 
     case R.id.close_button: 
      m_behaviour.setState(BottomSheetBehavior.STATE_COLLAPSED); 
      break; 
    } 
} 

}

任何意见,将不胜感激。

+0

http://stackoverflow.com/questions/39030742/bottomsheetbehavior-is-not-work-when-design-library-update-to-24-2 -0 –

回答

11
m_behaviour.setPeekHeight(0); 

它默认为“窥视”的状态,所以如果你不希望它在所有偷看,你需要把偷看的高度设置为0

+0

谢谢,斯科特。它在最初设置m_behaviour.setPeekHeight(0)时按预期工作,例如,在onCreate() –

2
app:behavior_peekHeight="0dp" 
app:layout_behavior="@string/bottom_sheet_behavior" 

您可以设置在布局中偷看高度为0dp,不需要以编程方式设置