I will give you an example to explain the bottom sheet fragment template generated from android studio.
The result of this example is popping up a bottom sheet after click the button in the main activity.
Firstly, we can create the main activity as below witch a button to call the bottom sheet and a text view to show the result after clicking the item in the bottom sheet:
Next, we generate the bottom sheet fragment template from android studio by clicking File -> New -> Fragment -> Modal Bottom Sheet
After click the Modal Bottom Sheet, android studio will automatically generate three files for you, show as below:
ItemListDialogFragment: this is a java class which can render the recycler view and bottom sheet fragment.
fragment_item_list_dialog.xml: this is a layout of recycler view in the bottom sheet fragment (Also the layout of bottom sheet fragment) // you can change this file to change the layout of bottom sheet fragment.
fragment_item_list_dialog_item.xml: this is a view insert into the recycler view. // if you don’t use the recycler view, you can delete this file.
We don’t care the layout of the bottom sheet fragment, so we just keep the fragment_item_list_dialog.xml and fragment_item_list_dialog_item.xml. We will focus on the ItemListDialogFragment class.
Blow is the template of Modal Bottom Sheet Fragment, I will give you the explanation in the code:
/** * <p>A fragment that shows a list of items as a modal bottom sheet.</p> * <p>You can show this modal bottom sheet from your activity like this:</p> * <pre> * ItemListDialogFragment.newInstance(30).show(getSupportFragmentManager(), "dialog"); * </pre> * <p>You activity (or fragment) needs to implement {@link ItemListDialogFragment.Listener}.</p> */ publicclassItemListDialogFragmentextendsBottomSheetDialogFragment{
// TODO: Customize parameter argument names // the String to put and get arguments to/from bundle privatestaticfinal String ARG_ITEM_COUNT = "item_count"; //The listener to listen when the text view is clicked private Listener mListener;
// TODO: Customize parameters // Construstor and put arguments to fragment publicstatic ItemListDialogFragment newInstance(int itemCount){ final ItemListDialogFragment fragment = new ItemListDialogFragment(); final Bundle args = new Bundle(); //the itemCount means the input data to the RecyclerView adapter, you can change it to // String[] or ArrayList to bind the data to the recycler view args.putInt(ARG_ITEM_COUNT, itemCount); fragment.setArguments(args); return fragment; }
//onCreateView just render the recycler view in the fragment @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){ return inflater.inflate(R.layout.fragment_item_list_dialog, container, false); }
//onViewCreated: set the apdater and LayoutManager to recyclerview @Override publicvoidonViewCreated(View view, @Nullable Bundle savedInstanceState){ final RecyclerView recyclerView = (RecyclerView) view; recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setAdapter(new ItemAdapter(getArguments().getInt(ARG_ITEM_COUNT))); }
// When attach to the parent, get the listener from parent @Override publicvoidonAttach(Context context){ super.onAttach(context); final Fragment parent = getParentFragment(); if (parent != null) { mListener = (Listener) parent; } else { mListener = (Listener) context; } }
//When close the application, clear the listener @Override publicvoidonDetach(){ mListener = null; super.onDetach(); }
//this is a listener pending to implement in the parent publicinterfaceListener{ voidonItemClicked(int position); }
// custom ViewHolder in the recycler view's adapter's onCreateViewHolder() // and set listener to the item in the recycler view privateclassViewHolderextendsRecyclerView.ViewHolder{
final TextView text;
ViewHolder(LayoutInflater inflater, ViewGroup parent) { // TODO: Customize the item layout super(inflater.inflate(R.layout.fragment_item_list_dialog_item, parent, false)); text = (TextView) itemView.findViewById(R.id.text); text.setOnClickListener(new View.OnClickListener() { @Override publicvoidonClick(View v){ if (mListener != null) { mListener.onItemClicked(getAdapterPosition()); // When the item is clicked, the bottom sheet fragment disapear by // calling dismiss() dismiss(); } } }); }
privatefinalint mItemCount; //Recieve the data in the constructor ItemAdapter(int itemCount) { mItemCount = itemCount; }
//Create the custom ViewHolder @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){ returnnew ViewHolder(LayoutInflater.from(parent.getContext()), parent); }
//Bind data to the ViewHolder @Override publicvoidonBindViewHolder(ViewHolder holder, int position){ holder.text.setText(String.valueOf(position)); }
In this example, I will give a string array to the fragment, because we just want to show the finite options in the modal bottom sheet. And I will change the ItemListDialogFragment as below:
/** * <p>A fragment that shows a list of items as a modal bottom sheet.</p> * <p>You can show this modal bottom sheet from your activity like this:</p> * <pre> * ItemListDialogFragment.newInstance(30).show(getSupportFragmentManager(), "dialog"); * </pre> * <p>You activity (or fragment) needs to implement {@link ItemListDialogFragment.Listener}.</p> */ publicclassItemListDialogFragmentextendsBottomSheetDialogFragment{