Android "Top Sheet" 等同于 "Bottom Sheet"?
Android "Top Sheet" equivalent of "Bottom Sheet"?
我想实现一种 "Bottom Sheet" 类型的布局,但有一个扭曲,其中 "bottom" sheet 将是一个 MapFragment,它不会像up/down 可拖动视图。
我可能天真地想 "flip" "Top Sheet" 设计的逻辑,在其中拖动顶部 Sheet up/down 以显示 more/less底部 MapFragment.
即:从这个...
...[类似]这个...
如果有支持设计工具,这可能吗?还是我必须自己做这样的事情?
这是我在上面评论过的解决方案的基础。
稍后我会回来充实它。
@Override
protected void onCreate(
@Nullable
Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (isFinishing())
{
return;
}
setContentView(R.layout.activity_home);
...
mGroupBottomSheetFiller = (ViewGroup) findViewById(R.id.groupBottomSheetFiller);
final NestedScrollView bottomSheetMap = (NestedScrollView) findViewById(R.id.bottomSheetMap);
mBottomSheetMapBehavior = BottomSheetBehavior.from(bottomSheetMap);
mBottomSheetMapBehavior.setBottomSheetCallback(new BottomSheetCallback()
{
@Override
public void onStateChanged(
@NonNull
View bottomSheet,
int newState)
{
//Log.e(TAG, "mBottomSheetMapBehavior.onStateChanged(bottomSheet, newState=" +
// bottomSheetBehaviorStateToString(newState) + ')');
int visibility = isBottomSheetExpanded(mBottomSheetMapBehavior) ? View.VISIBLE : View.GONE;
mImageBottomSheetMapClose.setVisibility(visibility);
}
@Override
public void onSlide(
@NonNull
View bottomSheet,
float slideOffset)
{
//Log.e(TAG, "mBottomSheetMapBehavior.onStateChanged(bottomSheet, slideOffset=" + slideOffset + ')');
resizeMap();
}
});
bottomSheetMap.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
//Log.e(TAG, "onGlobalLayout()");
bottomSheetMap.getViewTreeObserver().removeOnGlobalLayoutListener(this);
resizeMap();
}
});
...
}
private void resizeMap()
{
int screenHeightPixels = PbPlatformUtils.getScreenHeightPixels();
//Log.e(TAG, "resizeMap: screenHeightPixels=" + screenHeightPixels);
int[] location = new int[2];
mGroupMap.getLocationInWindow(location);
//Log.e(TAG, "resizeMap: getLocationInWindow=" + Arrays.toString(location));
LayoutParams groupMapLayoutParams = mGroupMap.getLayoutParams();
groupMapLayoutParams.height = screenHeightPixels - location[1];
mGroupMap.requestLayout();
}
public static String bottomSheetBehaviorStateToString(int state)
{
String s;
switch (state)
{
case BottomSheetBehavior.STATE_COLLAPSED:
s = "STATE_COLLAPSED";
break;
case BottomSheetBehavior.STATE_DRAGGING:
s = "STATE_DRAGGING";
break;
case BottomSheetBehavior.STATE_EXPANDED:
s = "STATE_EXPANDED";
break;
case BottomSheetBehavior.STATE_HIDDEN:
s = "STATE_HIDDEN";
break;
case BottomSheetBehavior.STATE_SETTLING:
s = "STATE_SETTLING";
break;
default:
s = "UNKNOWN";
break;
}
return s + '(' + state + ')';
}
private static boolean isBottomSheetExpanded(
@NonNull
BottomSheetBehavior bottomSheetBehavior)
{
return bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED;
}
private void bottomSheetMapExpand()
{
mGroupBottomSheetFiller.setVisibility(View.VISIBLE);
int peekHeightPx = getResources().getDimensionPixelSize(R.dimen.home_bottom_sheet_map_peek_height);
mBottomSheetMapBehavior.setPeekHeight(peekHeightPx);
mBottomSheetMapBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
mBottomSheetMapBehavior.setHideable(false);
}
private void bottomSheetMapCollapse()
{
mGroupBottomSheetFiller.setVisibility(View.VISIBLE);
int peekHeightPx = getResources().getDimensionPixelSize(R.dimen.home_bottom_sheet_map_peek_height);
mBottomSheetMapBehavior.setPeekHeight(peekHeightPx);
mBottomSheetMapBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
mBottomSheetMapBehavior.setHideable(false);
}
private void bottomSheetMapHide()
{
mBottomSheetMapBehavior.setHideable(true);
mBottomSheetMapBehavior.setPeekHeight(0);
mBottomSheetMapBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
mGroupBottomSheetFiller.setVisibility(View.GONE);
}
我找到了一个 TopSheetBehavior
实现并试图使其保持最新:https://github.com/carlos-mg89/TopSheetBehavior
事实证明,它在我的案例中非常有效。我发现许多其他 TopSheetBehavior
不完整或崩溃了,但这个不会崩溃,只需替换行为参数即可开箱即用:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="56dp"
app:layout_behavior="your.package.components.TopSheetBehavior">
<!-- Your content goes here -->
</LinearLayout>
我想实现一种 "Bottom Sheet" 类型的布局,但有一个扭曲,其中 "bottom" sheet 将是一个 MapFragment,它不会像up/down 可拖动视图。
我可能天真地想 "flip" "Top Sheet" 设计的逻辑,在其中拖动顶部 Sheet up/down 以显示 more/less底部 MapFragment.
即:从这个...
...[类似]这个...
如果有支持设计工具,这可能吗?还是我必须自己做这样的事情?
这是我在上面评论过的解决方案的基础。 稍后我会回来充实它。
@Override
protected void onCreate(
@Nullable
Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (isFinishing())
{
return;
}
setContentView(R.layout.activity_home);
...
mGroupBottomSheetFiller = (ViewGroup) findViewById(R.id.groupBottomSheetFiller);
final NestedScrollView bottomSheetMap = (NestedScrollView) findViewById(R.id.bottomSheetMap);
mBottomSheetMapBehavior = BottomSheetBehavior.from(bottomSheetMap);
mBottomSheetMapBehavior.setBottomSheetCallback(new BottomSheetCallback()
{
@Override
public void onStateChanged(
@NonNull
View bottomSheet,
int newState)
{
//Log.e(TAG, "mBottomSheetMapBehavior.onStateChanged(bottomSheet, newState=" +
// bottomSheetBehaviorStateToString(newState) + ')');
int visibility = isBottomSheetExpanded(mBottomSheetMapBehavior) ? View.VISIBLE : View.GONE;
mImageBottomSheetMapClose.setVisibility(visibility);
}
@Override
public void onSlide(
@NonNull
View bottomSheet,
float slideOffset)
{
//Log.e(TAG, "mBottomSheetMapBehavior.onStateChanged(bottomSheet, slideOffset=" + slideOffset + ')');
resizeMap();
}
});
bottomSheetMap.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
//Log.e(TAG, "onGlobalLayout()");
bottomSheetMap.getViewTreeObserver().removeOnGlobalLayoutListener(this);
resizeMap();
}
});
...
}
private void resizeMap()
{
int screenHeightPixels = PbPlatformUtils.getScreenHeightPixels();
//Log.e(TAG, "resizeMap: screenHeightPixels=" + screenHeightPixels);
int[] location = new int[2];
mGroupMap.getLocationInWindow(location);
//Log.e(TAG, "resizeMap: getLocationInWindow=" + Arrays.toString(location));
LayoutParams groupMapLayoutParams = mGroupMap.getLayoutParams();
groupMapLayoutParams.height = screenHeightPixels - location[1];
mGroupMap.requestLayout();
}
public static String bottomSheetBehaviorStateToString(int state)
{
String s;
switch (state)
{
case BottomSheetBehavior.STATE_COLLAPSED:
s = "STATE_COLLAPSED";
break;
case BottomSheetBehavior.STATE_DRAGGING:
s = "STATE_DRAGGING";
break;
case BottomSheetBehavior.STATE_EXPANDED:
s = "STATE_EXPANDED";
break;
case BottomSheetBehavior.STATE_HIDDEN:
s = "STATE_HIDDEN";
break;
case BottomSheetBehavior.STATE_SETTLING:
s = "STATE_SETTLING";
break;
default:
s = "UNKNOWN";
break;
}
return s + '(' + state + ')';
}
private static boolean isBottomSheetExpanded(
@NonNull
BottomSheetBehavior bottomSheetBehavior)
{
return bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED;
}
private void bottomSheetMapExpand()
{
mGroupBottomSheetFiller.setVisibility(View.VISIBLE);
int peekHeightPx = getResources().getDimensionPixelSize(R.dimen.home_bottom_sheet_map_peek_height);
mBottomSheetMapBehavior.setPeekHeight(peekHeightPx);
mBottomSheetMapBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
mBottomSheetMapBehavior.setHideable(false);
}
private void bottomSheetMapCollapse()
{
mGroupBottomSheetFiller.setVisibility(View.VISIBLE);
int peekHeightPx = getResources().getDimensionPixelSize(R.dimen.home_bottom_sheet_map_peek_height);
mBottomSheetMapBehavior.setPeekHeight(peekHeightPx);
mBottomSheetMapBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
mBottomSheetMapBehavior.setHideable(false);
}
private void bottomSheetMapHide()
{
mBottomSheetMapBehavior.setHideable(true);
mBottomSheetMapBehavior.setPeekHeight(0);
mBottomSheetMapBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
mGroupBottomSheetFiller.setVisibility(View.GONE);
}
我找到了一个 TopSheetBehavior
实现并试图使其保持最新:https://github.com/carlos-mg89/TopSheetBehavior
事实证明,它在我的案例中非常有效。我发现许多其他 TopSheetBehavior
不完整或崩溃了,但这个不会崩溃,只需替换行为参数即可开箱即用:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="56dp"
app:layout_behavior="your.package.components.TopSheetBehavior">
<!-- Your content goes here -->
</LinearLayout>