Страницы

Поиск по вопросам

вторник, 18 июня 2019 г.

Неадекватное поведение BottomSheet

При инициализации BottomSheet оно частично отображается снизу экрана, далее при попытке подтащить его к верху, что бы оно полностью отобразилось, оно соскальзывает и исчезает. Как это исправить? Или как сделать, что бы появлялся сразу весь BottomSheet на экране?
Видео, как это выглядит: https://www.youtube.com/watch?v=58bhlc-KfYA&feature=youtu.be
код Активити:
public class FirstscreenActivity extends AppCompatActivity implements RecyclerItemClickListener.OnItemClickListener, ItemAdapter.ItemListener {
private BottomSheetDialog mBottomSheetDialog; BottomSheetBehavior behavior; private ItemAdapter mAdapterItem; private FloatingActionButton floatButton;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.front);
mList = (RecyclerView) findViewById(R.id.list); mList.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getApplicationContext()); mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); mList.addOnItemTouchListener(new RecyclerItemClickListener(this, this));
mList.setLayoutManager(mLayoutManager);
floatButton = (FloatingActionButton) findViewById(R.id.float_button); floatButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showBottomSheetDialog(); } });
View bottomSheet = findViewById(R.id.bottom_sheet; behavior = BottomSheetBehavior.from(bottomSheet); behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { // React to state change }
@Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { // React to dragging events } }); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapterItem = new ItemAdapter(createItems(), this); recyclerView.setAdapter(mAdapterItem);
}
@Override protected void onResume() { super.onResume(); RecyclerViewAdapter adapter = (RecyclerViewAdapter) mList.getAdapter(); adapter.notifyDataSetChanged(); }
private void showBottomSheetDialog() { if (behavior.getState() == BottomSheetBehavior.STATE_EXPANDED) { behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); }
mBottomSheetDialog = new BottomSheetDialog(this); View view = getLayoutInflater().inflate(R.layout.sheet, null); RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new ItemAdapter(createItems(), new ItemAdapter.ItemListener() { @Override public void onItemClick(Item item) { if (mBottomSheetDialog != null) { mBottomSheetDialog.dismiss(); } } }));
mBottomSheetDialog.setContentView(view); mBottomSheetDialog.show(); mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { mBottomSheetDialog = null; } }); }
@Override protected void onDestroy() { super.onDestroy(); mAdapterItem.setListener(null); }
public List createItems() { ArrayList items = new ArrayList<>(); items.add(new Item(R.drawable.camera, "from new shoots")); items.add(new Item(R.drawable.folder_multiple_image, "from ready images")); return items; }
@Override public void onItemClick(Item item) { behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); } }
Адаптер RecyclerView для BottomSheet:
public class ItemAdapter extends RecyclerView.Adapter {
private List mItems; private ItemListener mListener;
public ItemAdapter(List items, ItemListener listener) { mItems = items; mListener = listener; }
public void setListener(ItemListener listener) { mListener = listener; }
@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(LayoutInflater.from(parent.getContext()) .inflate(R.layout.adapter, parent, false)); }
@Override public void onBindViewHolder(ViewHolder holder, int position) { holder.setData(mItems.get(position)); }
@Override public int getItemCount() { return mItems.size(); }
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView imageView; public TextView textView; public Item item;
public ViewHolder(View itemView) { super(itemView); itemView.setOnClickListener(this); imageView = (ImageView) itemView.findViewById(R.id.imageView); textView = (TextView) itemView.findViewById(R.id.textView); }
public void setData(Item item) { this.item = item; imageView.setImageResource(item.getDrawableResource()); textView.setText(item.getTitle()); }
@Override public void onClick(View v) { if (mListener != null) { mListener.onItemClick(item); } } }
public interface ItemListener { void onItemClick(Item item); } }
xml активити:









xml RecyclerView Item:




Ответ

Хм, вопрос хороший!
Попробуйте указать следующее:
mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);

Комментариев нет:

Отправить комментарий