<pre>
public class BottomSheetBehaviorActivity extends AppCompatActivity {
private BottomSheetBehavior mBottomSheetBehavior;
private BottomSheetDialog mBottomSheetDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bsbehavior_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
findViewById(R.id.btn_bottom_sheet_control).setOnClickListener(onClickListener);
findViewById(R.id.btn_bottom_dialog_control).setOnClickListener(onClickListener);
mBottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.tab_layout));
createBottomSheetDialog();
}
private void createBottomSheetDialog() {
mBottomSheetDialog = new BottomSheetDialog(this);
View view = LayoutInflater.from(this).inflate(R.layout.dialog_bottom_sheet, null, false);
mBottomSheetDialog.setContentView(view);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
List<String> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add("我是第" + i + "个");
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setSmoothScrollbarEnabled(true);
recyclerView.setLayoutManager(linearLayoutManager);
ListRecyclerAdapter adapter = new ListRecyclerAdapter(list);
recyclerView.setAdapter(adapter);
setBehaviorCallback();
}
private void setBehaviorCallback() {
View view = mBottomSheetDialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet);
final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(view);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
mBottomSheetDialog.dismiss();
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_bottom_sheet_control) {
System.out.println("sss:" + mBottomSheetBehavior.getState());
//api25 时这样写!!一开始的状态默认为STATE_COLLAPSED,然后经过设置为STATE_HIDDEN,然后再经过设置为STATE_EXPANDED,然后再次回到STATE_EXPANDED上,所以可以写或运算
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED || mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
mBottomSheetBehavior.setHideable(true);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
} else if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
} else if (v.getId() == R.id.btn_bottom_dialog_control) {
if (mBottomSheetDialog.isShowing()) {
mBottomSheetDialog.dismiss();
} else {
mBottomSheetDialog.show();
}
}
}
};
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return true;
}
}
</pre>
网友评论