本文共 2465 字,大约阅读时间需要 8 分钟。
//按钮要使用布局包裹否则会浮在BottomSheetBehavior之上
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final BottomSheetBehavior bottomSheetBehavior=BottomSheetBehavior.from(findViewById(R.id.design_bottom_sheet1)); //设置默认先隐藏 bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //根据状态不同显示隐藏 if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) { bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); } else if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) { bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); } } }); //设置监听事件 bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { //拖动 } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { //状态变化 } }); }}
public class BottomSheetDialogFragmenttest extends BottomSheetDialogFragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.test,container,false); }}
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { BottomSheetDialogFragmenttest bottomSheetDialogFragmenttest=new BottomSheetDialogFragmenttest(); bottomSheetDialogFragmenttest.show(getSupportFragmentManager(),BottomSheetDialogFragmenttest.class.getSimpleName()); } });
转载地址:http://csrxx.baihongyu.com/