我正在尝试遵循“材质设计” specification,使用Android Design支持库在模态底页中添加封闭式工具栏。
全屏显示时,可以在内部滚动底部的页面以显示其他内容。 应使用工具栏提供折叠或关闭功能以退出此视图。
我和BottomSheetBehavior.BottomSheetCallback
一起根据BottomSheetBehavior
的展开/折叠状态来切换工具栏的可见性。问题是,即使BottomSheetDialogFragment
内容无法填满整个屏幕,当我尝试向上拖动时,也会出现工具栏。如何知道底部面板展开时底部面板是全屏的?
public class BottomSheetToolbarToggleCallback : BottomSheetBehavior.BottomSheetCallback
{
public BottomSheetToolbarToggleCallback(BottomSheetDialogFragment bottomSheetDialogFragment)
{
this.bottomSheetDialogFragment = bottomSheetDialogFragment ?? throw new System.ArgumentNullException(nameof(bottomSheetDialogFragment));
}
public override void OnSlide(View bottomSheet, float slideOffset)
{
}
public override void OnStateChanged(View bottomSheet, int newState)
{
switch (newState)
{
case BottomSheetBehavior.StateCollapsed:
ShowToolbar(bottomSheet, ViewStates.Gone);
break;
case BottomSheetBehavior.StateExpanded:
ShowToolbar(bottomSheet, ViewStates.Visible);
break;
case BottomSheetBehavior.StateHidden:
bottomSheetDialogFragment.Dismiss();
break;
}
}
private void ShowToolbar(View bottomSheet, ViewStates viewState)
{
var toolbar = bottomSheet.FindViewById<Toolbar>(Resource.Id.toolbar);
if (toolbar != null)
{
toolbar.Visibility = viewState;
}
}
private readonly BottomSheetDialogFragment bottomSheetDialogFragment;
}
public abstract class BaseBottomSheetDialogFragment<TViewModel> : MvxBottomSheetDialogFragment<TViewModel> where TViewModel : BaseViewModel
{
protected BaseBottomSheetDialogFragment()
{
}
protected BaseBottomSheetDialogFragment(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public override void SetupDialog(Dialog dialog, int style)
{
base.SetupDialog(dialog, style);
this.EnsureBindingContextIsSet(); // This is required to use this.BindingInflate()
var view = this.BindingInflate(LayoutResourceId, null);
dialog.SetContentView(view);
// Add support to handle material design specification to dynamically show a toolbar with an 'X' button.
var layoutParams = (CoordinatorLayout.LayoutParams)((View)view.Parent).LayoutParameters;
var behavior = layoutParams.Behavior;
if (behavior != null && behavior is BottomSheetBehavior)
{
var toolbar = view.FindViewById<Toolbar>(Droid.Resource.Id.toolbar);
if (toolbar != null)
{
toolbar.SetNavigationIcon(Droid.Resource.Drawable.clear);
((BottomSheetBehavior)behavior).SetBottomSheetCallback(new BottomSheetToolbarToggleCallback(this));
if (CloseCommand != null)
{
toolbar.SetNavigationOnClickListener(new MvxAsyncCommandOnClickListener(CloseCommand));
}
}
}
}
/// <summary>
/// The Android layout resource id of the layout to show in the modal bottom sheet.
/// </summary>
protected abstract int LayoutResourceId { get; }
/// <summary>
/// Optional <see cref="MvxAsyncCommand"/> to call when the optional toolbar navigation button is clicked.
/// </summary>
protected abstract IMvxAsyncCommand CloseCommand { get; }
}
答案 0 :(得分:1)
由于我不知道您的特定代码,因此以下是我尝试过的一个简单示例的片段:
public View mView;
public override void SetupDialog(Dialog dialog, int style)
{
base.SetupDialog(dialog, style);
this.EnsureBindingContextIsSet(); // This is required to use this.BindingInflate()
mView = this.BindingInflate(LayoutResourceId, null);
dialog.SetContentView(view);
...
}
在BottomSheetCallback
中:
public override void OnStateChanged(View bottomSheet, int newState)
{
switch (newState)
{
case BottomSheetBehavior.StateExpanded:
var height = bottomSheetDialogFragment.mView.Height;//need you convert to your BottomSheetDialogFragment
break;
}
}
答案 1 :(得分:0)
这是我最终使用的解决方案。仅当BottomSheetDialogFragment
占据窗口的整个高度时,它才会显示关闭能力。它已经过多窗口模式,切口/插入,横向和纵向方向的测试。
public class BottomSheetDialogFragmentCloseAffordanceCallback : BottomSheetBehavior.BottomSheetCallback
{
public BottomSheetDialogFragmentCloseAffordanceCallback(BottomSheetDialogFragment bottomSheetDialogFragment)
{
this.bottomSheetDialogFragment = bottomSheetDialogFragment ?? throw new System.ArgumentNullException(nameof(bottomSheetDialogFragment));
}
public override void OnSlide(View bottomSheet, float slideOffset)
{
}
public override void OnStateChanged(View bottomSheet, int newState)
{
switch (newState)
{
case BottomSheetBehavior.StateCollapsed:
ShowToolbar(bottomSheet, ViewStates.Gone);
break;
case BottomSheetBehavior.StateExpanded:
if (IsViewSameHeightAsWindow(bottomSheet))
{
ShowToolbar(bottomSheet, ViewStates.Visible);
}
break;
case BottomSheetBehavior.StateHalfExpanded:
break;
case BottomSheetBehavior.StateHidden:
bottomSheetDialogFragment.Dismiss();
break;
}
}
private void ShowToolbar(View bottomSheet, ViewStates viewState)
{
var toolbar = bottomSheet.FindViewById<Toolbar>(Resource.Id.toolbar);
if (toolbar != null)
{
toolbar.Visibility = viewState;
}
}
private bool IsViewSameHeightAsWindow(View view)
{
int[] locationInWindow = new int[2];
view.GetLocationInWindow(locationInWindow);
return locationInWindow[1] == view.RootWindowInsets.StableInsetTop;
}
private readonly BottomSheetDialogFragment bottomSheetDialogFragment;
}