Selection of menu bar symbols
There are many menu symbols in Android studio. Select drawable
You can choose any symbol you want
code implementation
public class CrimeListFragment extends Fragment {
public static final String SUBTITLE = "subtitle";
private RecyclerView mRecyclerView;
private CrimeAdpater mCrimeAdpater;
private boolean mSubtitleVisible;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Accept menu options callback
setHasOptionsMenu(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.crime_list_fragment,container,false);
if(savedInstanceState!=null){
mSubtitleVisible=savedInstanceState.getBoolean(SUBTITLE);
}
mRecyclerView=(RecyclerView)view.findViewById(R.id.crime_recycler);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView list_title;
private TextView list_date;
Crime mCrime;
public void bind(Crime crime){
mCrime=crime;
list_title.setText(crime.getTitle());
list_date.setText(crime.getDate().toString());
}
public CrimeHolder(LayoutInflater inflater,ViewGroup parent){
super(inflater.inflate(R.layout.crime_list_item,parent,false));
list_title=(TextView)itemView.findViewById(R.id.list_title);
list_date=(TextView)itemView.findViewById(R.id.list_date);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// Toast.makeText(getActivity(), mCrime.getTitle()+"click!", Toast.LENGTH_SHORT).show();
// Intent intent= new Intent(getActivity(),CrimeActivity.class);
// Intent intent=CrimeActivity.newIntent(getActivity(),mCrime.getId());
Intent intent= CrimePagerActivity.newIntent(getActivity(),mCrime.getId());
startActivity(intent);
}
}
private class CrimeAdpater extends RecyclerView.Adapter<CrimeHolder>{
private List<Crime> mCrimes;
public CrimeAdpater(List<Crime> crimes){
mCrimes=crimes;
}
@Override
public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
return new CrimeHolder(layoutInflater,parent);
}
@Override
public void onBindViewHolder(CrimeHolder holder, int position) {
Crime crime=mCrimes.get(position);
holder.bind(crime);
}
@Override
public int getItemCount() {
return mCrimes.size();
}
}
@Override
public void onResume() {
super.onResume();
updateUI();
}
private void updateUI(){
CrimeLab crimeLab=CrimeLab.getCrimeLab(getActivity());
List<Crime> crimes=crimeLab.getCrimes();
// mCrimeAdpater=new CrimeAdpater(crimes);
// mRecyclerView.setAdapter(mCrimeAdpater);
if(mCrimeAdpater==null){
mCrimeAdpater=new CrimeAdpater(crimes);
mRecyclerView.setAdapter(mCrimeAdpater);
}else{
mCrimeAdpater.notifyDataSetChanged();
}
updateSubtitle();
}
//Create menu, import menu resource
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_crime_list,menu);
MenuItem subtitleItem=menu.findItem(R.id.show_subtitle);
if(mSubtitleVisible){
subtitleItem.setTitle("show");
}else{
subtitleItem.setTitle("hide");
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.new_crime:
Crime crime=new Crime();
CrimeLab.getCrimeLab(getActivity()).addCrime(crime);
Intent intent=CrimePagerActivity.newIntent(getActivity(),crime.getId());
startActivity(intent);
return true;
case R.id.show_subtitle:
mSubtitleVisible=!mSubtitleVisible;
getActivity().invalidateOptionsMenu();
updateSubtitle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//Show how many items there are
public void updateSubtitle(){
CrimeLab crimeLab=CrimeLab.getCrimeLab(getActivity());
int crimeCount=crimeLab.getCrimes().size();
String subtitle=getString(R.string.subtitle_format,crimeCount);
if(!mSubtitleVisible){
subtitle=null;
}
AppCompatActivity activity=(AppCompatActivity)getActivity();
activity.getSupportActionBar().setSubtitle(subtitle);
}
//When the screen rotates, the data is lost. Save it through the bundle and get it in the onCreateView method
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SUBTITLE,mSubtitleVisible);
}
}
Create menu file
Select res
Finally, the folder of menu and its xml file will be generated automatically
Code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/new_crime"
android:icon="@drawable/ic_menu_add"
android:title="@string/new_crime"
app:showAsAction="ifRoom|withText"/>
<item
android:id="@+id/show_subtitle"
android:title="@string/show_subtitle"
app:showAsAction="ifRoom"/>
</menu>
app:showAsAction="ifRoom|withText":
It means that as long as the space is enough, the menu bar icon and text description will be displayed on the toolbar. If the space is only enough to display the icon, the text description will not be displayed, or only enough to display the text description, the icon will not be displayed
ifroom: let the operating system decide how to display the menu bar