我有一个带有自定义视图和持久性底部工作表的CoordinatorLayout。我已禁用底部工作表的隐藏,并希望在折叠状态下显示LinearLayout中的顶部视图。为了考虑不同的屏幕尺寸,我会在所有内容布局完成后,在我的活动“onCreate”中动态设置peekHeight。
问题是当活动首次开始时,BottomSheet在一个高度偷看,然后在它再次展开和折叠后,peekHeight似乎已经改变了。有趣的是,它在视觉上看起来像peekHeight添加的是布局活动填充(16dp)。但是,BottomSheetBehavior为两者报告相同的peekHeight。
我为此创建了一个hacky解决方法,但我想知道是否有任何错误。下面是一个gif显示我没有我的解决方法和一个当我使用我的解决方法时。我还包括相关的代码和布局文件。再次,它似乎与我的填充(16dp)直接相关,但我不明白如何解决它。
不使用解决方法:
使用解决方法:
活动:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_schedule_details_activity);
final CoordinatorLayout activityLayout =
(CoordinatorLayout) findViewById(R.id.edit_schedule_details_layout);
ButterKnife.bind(this);
// ... removed unrelated logic
final View bottomSheet = findViewById(R.id.edit_details_sheet);
final LinearLayout peekContainer = (LinearLayout) findViewById(R.id.info_title_container);
activityLayout
.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Set the measured peek height and add in 16 dps to account for inconsistency
// Adding the 16 dps is part of the workaround
bottomSheetBehavior.setPeekHeight(peekContainer.getHeight());
//+ (int) (16 * getResources().getDisplayMetrics().density)); /* workaround */
Timber.i("Title height: " + bottomSheetBehavior.getPeekHeight());
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(View bottomSheet, int newState) {
String stateString = "";
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
stateString = "Expanded";
/* workaround */
/*if(!bottomSheetSet){
// remove the extra 16 dp added since the layout now looks correct
bottomSheetBehavior
.setPeekHeight(
bottomSheetBehavior.getPeekHeight()
- (int) (16 * getResources().getDisplayMetrics().density));
bottomSheetSet = true;
}*/
}
else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
stateString = "Collapsed";
Timber.i("peekHeight = " + bottomSheetBehavior.getPeekHeight());
}
else if (newState == BottomSheetBehavior.STATE_HIDDEN) {
stateString = "Hidden";
}
else if(newState == BottomSheetBehavior.STATE_SETTLING) {
stateString = "Settling";
}
else if(newState == BottomSheetBehavior.STATE_DRAGGING){
stateString = "Dragging";
}
Timber.i("bottomSheetState = " + stateString);
}
@Override
public void onSlide(View bottomSheet, float slideOffset) {
}
});
}
布局:
<?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"
android:id="@+id/edit_schedule_details_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin"
android:background="#ff171e26">
<com.cel.cortetcommercial.widget.ring_view.RingView
android:id="@+id/edit_ringview"
android:layout_width="match_parent"
android:layout_height="400dip"
app:inner_radius="95dip"
app:outer_radius="130dip"
app:color="@color/colorSecondaryDark"
app:is_touchable="true"
app:text_size="12dip"
app:tick_length="0dip"
app:tick_width="0dip"
android:transitionName="@string/transition_details_to_edit"/>
<LinearLayout
android:id="@+id/add_remove_button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
app:layout_anchor="@id/edit_ringview"
app:layout_anchorGravity="bottom">
<Button
android:id="@+id/remove_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_gravity="end"/>
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/edit_details_sheet"
android:layout_width="match_parent"
android:layout_height="250dp"
android:clipToPadding="true"
android:background="@color/colorPrimary"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
app:behavior_hideable="false">
<include layout="@layout/current_info_sheet">
</include>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
current_info_sheet:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/info_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/info_title_container">
<TextView
android:id="@+id/segment_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:text="@string/placeholder"
android:textAppearance="@android:style/TextAppearance.Material.Headline"
android:textColor="@color/white"/>
<TextView
android:id="@+id/time_remaining"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:text="@string/placeholder"
android:textColor="@color/white"/>
<Space
android:layout_width="1dp"
android:layout_height="16dp"/>
</LinearLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:textColor="@color/white"
android:text="@string/luminosity"/>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:textColor="@color/white"
android:text="@string/color_warmth"/>
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"/>
</LinearLayout>