下面列出了怎么用android.support.design.widget.BottomSheetBehavior的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Window window = requireActivity().getWindow();
if (window != null) {
if (Build.VERSION.SDK_INT >= 21) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
requireNavigationScene().addOnBackPressedListener(this, new OnBackPressedListener() {
@Override
public boolean onBackPressed() {
if (mBehavior.getState() != BottomSheetBehavior.STATE_COLLAPSED) {
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
return true;
}
return false;
}
});
}
private void showAccountNotExistDialog(BaseInvokeModel baseInvokeModel, BaseInfo baseInfo) {
bottomSheetDialog = new BottomSheetDialog(MainActivity.this);
DialogAuthorAccountNotExistBinding binding = DataBindingUtil.inflate(LayoutInflater.from(Utils.getContext()), R.layout.dialog_author_account_not_exist, null, false);
bottomSheetDialog.setContentView(binding.getRoot());
// 设置dialog 完全显示
View parent = (View) binding.getRoot().getParent();
BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
binding.getRoot().measure(0, 0);
behavior.setPeekHeight(binding.getRoot().getMeasuredHeight());
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
parent.setLayoutParams(params);
bottomSheetDialog.setCanceledOnTouchOutside(false);
final ConfrimDialogViewModel confrimDialogViewModel = new ConfrimDialogViewModel(getApplication());
confrimDialogViewModel.setBaseInfo(baseInvokeModel, baseInfo);
binding.setViewModel(confrimDialogViewModel);
bottomSheetDialog.show();
}
private void setupBottomSheetDialog() {
mBottomSheetDialog = new BottomSheetDialog(mContext);
View dialogView = LayoutInflater.from(mContext)
.inflate(R.layout.dialog_bottom_sheet_profile, null);
RecyclerView avatarRecycler = (RecyclerView) dialogView.findViewById(R.id.avatar_recycler);
avatarRecycler.setHasFixedSize(true);
avatarRecycler.setLayoutManager(new GridLayoutManager(mContext, 3, GridLayoutManager.VERTICAL, false));
avatarRecycler.setAdapter(new AvatarAdapter(mContext, this));
mBottomSheetDialog.setContentView(dialogView);
dialogView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) dialogView.getParent());
mBehavior.setPeekHeight(dialogView.getMeasuredHeight() + 200);
mBottomSheetDialog.show();
}
private void getData() {
mAmap = mMapView.getMap();
mSearchInteracter = new SearchInteracter(this, TypeMap.TYPE_AMAP);
mAmap.setOnMarkerClickListener(this);
mAmap.setOnMapLoadedListener(this);
mAmap.setOnCameraChangeListener(this);
// 开启定位图层
mAmap.setMyLocationEnabled(true);
// 开启室内图
mAmap.showIndoorMap(true);
mAmap.setOnMyLocationChangeListener(this);
mAmap.getUiSettings().setMyLocationButtonEnabled(false);
Bundle bundle = getExtras();
if (null != bundle) {
mBusRoute = bundle.getParcelable("bus");
mBusRouteResult = bundle.getParcelable("route");
}
mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
private void initToolbar(final boolean fullScreenOnly) {
toolbar.setNavigationIcon(R.drawable.belvedere_ic_close);
toolbar.setNavigationContentDescription(R.string.belvedere_toolbar_desc_collapse);
toolbar.setBackgroundColor(Color.WHITE);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!fullScreenOnly) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
} else {
dismiss();
}
}
});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
toolbarCompatShadow.setVisibility(View.VISIBLE);
}
CoordinatorLayout.LayoutParams layoutParams =
(CoordinatorLayout.LayoutParams) toolbarContainer.getLayoutParams();
if(layoutParams != null) {
layoutParams.setBehavior(new ToolbarBehavior(!fullScreenOnly));
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog bottomSheetDialog1 = (BottomSheetDialog) dialog;
FrameLayout bottomSheet
= bottomSheetDialog1.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(true);
BottomSheetBehavior.from(bottomSheet).setHideable(true);
}
});
return bottomSheetDialog;
}
protected void showInfo(String header, String desc, @Nullable Drawable drawable, boolean error) {
hideKeyboard();
AppCompatTextView infoHeader = (AppCompatTextView) llBottomSheet.findViewById(R.id.info_header);
AppCompatTextView infoDesc = (AppCompatTextView) llBottomSheet.findViewById(R.id.info_desc);
AppCompatImageView infoImg = (AppCompatImageView) llBottomSheet.findViewById(R.id.info_img);
if (error) {
llBottomSheet.findViewById(R.id.info_img).setVisibility(View.GONE);
((AppCompatButton) findViewById(R.id.ok_dimiss)).setText("Close");
} else {
llBottomSheet.findViewById(R.id.info_img).setVisibility(View.VISIBLE);
((AppCompatButton) findViewById(R.id.ok_dimiss)).setText("Ok");
}
infoHeader.setText(header);
infoDesc.setText(desc);
if (drawable != null)
infoImg.setImageDrawable(drawable);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.my_bottom_sheet_dialog_fragment, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
bottomSheetBehavior = (BottomSheetBehavior) behavior;
bottomSheetBehavior.setBottomSheetCallback(bottomSheetBehaviorCallback);
bottomSheetBehavior.setPeekHeight(200);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
bottomSheetBehavior = BottomSheetBehavior.from(this);
bottomSheetBehavior.setPeekHeight((int) getResources().getDimension(R.dimen.fluentappbar_bar_height));
bottomSheetBehavior.setHideable(false);
bottomSheetBehavior.setBottomSheetCallback(bottomSheetCallback);
View moreIcon = findViewWithTag(MORE_ICON_TAG);
moreIcon.setOnClickListener(onMoreClickListener);
if (fluentAppBarType == FULL_FLUENT) {
handleShowFluentBlur();
}
if (fluentAppBarType == DISABLE_FLUENT) {
keepFluentRipple = false;
}
}
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
final ViewRevealManager revealManager = new ViewRevealManager();
final SpringViewAnimatorManager springManager = new SpringViewAnimatorManager();
springManager.setDampingRatio(SpringForce.DAMPING_RATIO_NO_BOUNCY);
springManager.setStiffness(SpringForce.STIFFNESS_LOW);
parent.setViewRevealManager(revealManager);
settingsView.addSwitch("Enable Spring", false, new CompoundButton.OnCheckedChangeListener() {
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
parent.setViewRevealManager(isChecked ? springManager : revealManager);
}
});
settingsView.setAnimatorManager(springManager);
final BottomSheetBehavior behavior = BottomSheetBehavior.from(settingsView);
behavior.setPeekHeight(getResources().getDimensionPixelSize(R.dimen.bottom_peek_height));
behavior.setSkipCollapsed(false);
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
private void setupBottomSheetDialog() {
mBottomSheetDialog = new BottomSheetDialog(mContext);
View dialogView = LayoutInflater.from(mContext)
.inflate(R.layout.dialog_bottom_sheet_profile, null);
RecyclerView avatarRecycler = (RecyclerView) dialogView.findViewById(R.id.avatar_recycler);
avatarRecycler.setHasFixedSize(true);
avatarRecycler.setLayoutManager(new GridLayoutManager(mContext, 3, GridLayoutManager.VERTICAL, false));
avatarRecycler.setAdapter(new AvatarAdapter(mContext, this));
mBottomSheetDialog.setContentView(dialogView);
dialogView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) dialogView.getParent());
mBehavior.setPeekHeight(dialogView.getMeasuredHeight() + 200);
mBottomSheetDialog.show();
}
private static void toggleBottomSheet(View bottomSheet) {
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
int state = bottomSheetBehavior.getState();
switch (state) {
case BottomSheetBehavior.STATE_COLLAPSED:
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
break;
case BottomSheetBehavior.STATE_HIDDEN:
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
break;
case BottomSheetBehavior.STATE_EXPANDED:
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
break;
}
}
private void expandSheet(DialogInterface dialog)
{
if (dialog == null) {
return;
}
BottomSheetDialog bottomSheet = (BottomSheetDialog) dialog;
FrameLayout layout = (FrameLayout) bottomSheet.findViewById(android.support.design.R.id.design_bottom_sheet); // for AndroidX, resource is renamed to com.google.android.material.R.id.design_bottom_sheet
if (layout != null)
{
BottomSheetBehavior behavior = BottomSheetBehavior.from(layout);
behavior.setHideable(true);
behavior.setSkipCollapsed(true);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
@SuppressLint("InflateParams")
private BottomSheetMenu(@NonNull Context context, BottomSheetMenuListener bottomSheetMenuListener) {
super(context);
mIconSize = context.getResources().getDimensionPixelSize(R.dimen.bottom_sheet_menu_item_icon_size);
mBottomSheetMenuListener = bottomSheetMenuListener;
setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
if (bottomSheet != null) {
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
});
}
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
View view = View.inflate(getContext(), R.layout.dialog_bottom_sheet, null);
dialog.setContentView(view);
mBehavior = BottomSheetBehavior.from((View) view.getParent());
return dialog;
}
public static void delayDismiss(final BottomSheetBehavior behavior) {
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
}, 300);
}
private void expandSheet(DialogInterface dialog)
{
if (dialog != null)
{
BottomSheetDialog bottomSheet = (BottomSheetDialog) dialog;
FrameLayout layout = (FrameLayout) bottomSheet.findViewById(android.support.design.R.id.design_bottom_sheet); // for AndroidX, resource is renamed to com.google.android.material.R.id.design_bottom_sheet
if (layout != null)
{
BottomSheetBehavior behavior = BottomSheetBehavior.from(layout);
behavior.setHideable(true);
behavior.setSkipCollapsed(true);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (R.id.action_close == id) {
if (BApp.TYPE_MAP == TypeMap.TYPE_BAIDU && null != baiduMapFragment && baiduMapFragment.isModeRanging()) {
changeModeRanging(false);
} else if (BApp.TYPE_MAP == TypeMap.TYPE_AMAP && null != mAmapFragment && mAmapFragment.isModeRanging()) {
changeModeRanging(false);
}
if (BApp.TYPE_MAP == TypeMap.TYPE_BAIDU && null != baiduMapFragment) {
baiduMapFragment.clearMarker();
} else if (BApp.TYPE_MAP == TypeMap.TYPE_AMAP && null != mAmapFragment) {
mAmapFragment.clearMarker();
}
showPoiLay(null, -1);
mBehaviorSearchRseult.setState(BottomSheetBehavior.STATE_HIDDEN);
menuItemClose.setVisible(false);
textSearch.setHint("搜索地点");
mSearchPoiResultAdapter = null;
mRecycleResult.setAdapter(null);
} else if (R.id.action_clear == id) {
clearRangingPoi();
} else if (R.id.action_delete == id) {
deleteRangingPoi();
} else if (R.id.action_real_time_traffic == id) {
changeTraffic(item);
} else if (R.id.action_look_angle == id) {
changeAngle(item);
} else if (R.id.action_satellite_map == id) {
changeMapType(item);
}
return super.onOptionsItemSelected(item);
}
private void initBehavior() {
mBottomSheetBehavior = BottomSheetBehavior.from(mBottomSheet);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetCallback);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}, BOTTOM_SHEET_ENTER_ANIMATION_OFFSET);
}
private void updateBottomSheetPosition() {
activeTab = tabLayout.getSelectedTabPosition();
if (activeTab != 2) {
mBottomSheetBehavior.setPeekHeight(1);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
//Log.w("myApp", "[#] GPSActivity.java - mBottomSheetBehavior.setPeekHeight(" + bottomSheet.getHeight() +");");
mBottomSheetBehavior.setPeekHeight(bottomSheet.getHeight());
} else {
mBottomSheetBehavior.setPeekHeight(1);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED) ;
}
}
public boolean onBackPressed() {
if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
return false;
}
return true;
}
private void routeLine(TypeNavigation type) {
setRouteDetailsAdapter(null);
mTextInfo.setText("");
mTextDuration.setText("");
mLayPlan0.setVisibility(View.GONE);
mLayPlanAll.setVisibility(View.GONE);
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
if (null != mPoiStart && null != mPoiEnd) {
if (null != mBaiduMap) {
mBaiduMap.clear();
}
PlanNode stNode = PlanNode.withLocation(new LatLng(mPoiStart.getLatitude(), mPoiStart.getLongitude()));
PlanNode enNode = PlanNode.withLocation(new LatLng(mPoiEnd.getLatitude(), mPoiEnd.getLongitude()));
RoutePlanSearch planSearch = RoutePlanSearch.newInstance();
planSearch.setOnGetRoutePlanResultListener(this);
if (type == TypeNavigation.WALK) {
planSearch.walkingSearch(new WalkingRoutePlanOption().from(stNode).to(enNode));
} else if (type == TypeNavigation.BUS) {
planSearch.transitSearch(new TransitRoutePlanOption().from(stNode).to(enNode).city(mPoiStart.getCity()));
} else if (type == TypeNavigation.BIKE) {
planSearch.bikingSearch(new BikingRoutePlanOption().from(stNode).to(enNode));
} else if (type == TypeNavigation.DRIVE) {
planSearch.drivingSearch(new DrivingRoutePlanOption().from(stNode).to(enNode)
.trafficPolicy(DrivingRoutePlanOption.DrivingTrafficPolicy.ROUTE_PATH_AND_TRAFFIC)
.policy(DrivingRoutePlanOption.DrivingPolicy.ECAR_AVOID_JAM));
}
}
}
@Override
public void onMapClick(LatLng latLng) {
((RouteActivity) getActivity()).showToolbar();
if (mBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (R.id.action_close == id) {
if (BApp.TYPE_MAP == TypeMap.TYPE_BAIDU && null != baiduMapFragment && baiduMapFragment.isModeRanging()) {
changeModeRanging(false);
} else if (BApp.TYPE_MAP == TypeMap.TYPE_AMAP && null != mAmapFragment && mAmapFragment.isModeRanging()) {
changeModeRanging(false);
}
if (BApp.TYPE_MAP == TypeMap.TYPE_BAIDU && null != baiduMapFragment) {
baiduMapFragment.clearMarker();
} else if (BApp.TYPE_MAP == TypeMap.TYPE_AMAP && null != mAmapFragment) {
mAmapFragment.clearMarker();
}
showPoiLay(null, -1);
mBehaviorSearchRseult.setState(BottomSheetBehavior.STATE_HIDDEN);
menuItemClose.setVisible(false);
textSearch.setHint("搜索地点");
mSearchPoiResultAdapter = null;
mRecycleResult.setAdapter(null);
} else if (R.id.action_clear == id) {
clearRangingPoi();
} else if (R.id.action_delete == id) {
deleteRangingPoi();
} else if (R.id.action_real_time_traffic == id) {
changeTraffic(item);
} else if (R.id.action_look_angle == id) {
changeAngle(item);
} else if (R.id.action_satellite_map == id) {
changeMapType(item);
}
return super.onOptionsItemSelected(item);
}
private void setSearchResultAdapter(int position, List<MyPoiModel> poiAll) {
if (null == mSearchPoiResultAdapter) {
mSearchPoiResultAdapter = new SearchResultRecyclerAdapter(this, poiAll, BApp.MY_LOCATION);
mSearchPoiResultAdapter.setOnSelectSearchResultListener(this);
mRecycleResult.setAdapter(mSearchPoiResultAdapter);
} else {
mSearchPoiResultAdapter.setList(poiAll);
mSearchPoiResultAdapter.notifyDataSetChanged();
}
mRecycleResult.scrollToPosition(position);
mBehaviorSearchRseult.setState(BottomSheetBehavior.STATE_EXPANDED);
menuItemClose.setVisible(true);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mDrawer.isDrawerOpen(Gravity.START)) {
mDrawer.closeDrawer(Gravity.START);
} else if (mBehaviorPoi.getState() == BottomSheetBehavior.STATE_EXPANDED) {
mBehaviorPoi.setState(BottomSheetBehavior.STATE_HIDDEN);
} else {
exitApp();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
private void initializeObjects() {
progressDialog = new ProgressDialog(this);
dataStore = new DataStore();
steemPostCreator = new SteemPostCreator();
steemPostCreator.setSteemPostCreatorCallback(this);
rankableCompetitionItemRecyclerAdapter = new RankableCompetitionItemRecyclerAdapter(this);
competitionWinnerSelectionList.setLayoutManager(new LinearLayoutManager(this));
rankableCompetitionItemRecyclerAdapter.setRankableCompetitionItemListener(this);
competitionWinnerSelectionList.setAdapter(rankableCompetitionItemRecyclerAdapter);
sheetBehavior = BottomSheetBehavior.from(bottomSheet);
sheetBehavior.setHideable(false);
shortlistedWinnersMap = new HashMap<>();
title.setText(String.format("Winners of: %s", mCompetitionTitle));
invalidateWinnerList();
}
@Override
public void onAssignRankClicked(RankableCompetitionFeedItem item) {
rankAssigneeContext = item;
if (sheetBehavior != null) {
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
private void resetBottomSheetState(int bottomSheetState) {
if (bottomSheetState > INVALID_STATE) {
boolean isShowing = bottomSheetState == BottomSheetBehavior.STATE_EXPANDED;
summaryBehavior.setHideable(!isShowing);
summaryBehavior.setState(bottomSheetState);
}
}