今天的任务为添加工具栏菜单,代码如下:
strings.xml:
<resources> <string name="app_name">Criminallntent</string> <string name="crime_title_hint">Enter a title for the crime.</string> <string name="crime_title_label">Title</string> <string name="crime_details_label">Details</string> <string name="crime_solved_label">Solved</string> <string name="date_picker_title">Date of crime:</string> <string name="new_crime">New Crime</string> <string name="show_subtitle">Show Subtitle</string> <string name="hide_subtitle">Hide Subtitle</string> <string name="subtitle_format">%1$d crimes</string> </resources>
fragment_crime_list.xml:
<?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"/> </menu>
CrimeListFragment.java:
package com.bignerdranch.android.criminallntent; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import org.w3c.dom.Text; import java.util.List; public class CrimeListFragment extends Fragment { private RecyclerView mCrimeRecyclerView; private CrimeAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_crime_list, container, false); mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view); mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); updateUI(); return view; } @Override public void onResume(){ super.onResume(); updateUI(); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){ super.onCreateOptionsMenu(menu,inflater); inflater.inflate(R.menu.fragment_cime_list,menu); } private void updateUI() { CrimeLab crimeLab=CrimeLab.get(getActivity()); List<Crime> crimes=crimeLab.getCrimes(); if(mAdapter==null) { mAdapter = new CrimeAdapter(crimes); mCrimeRecyclerView.setAdapter(mAdapter); }else{ mAdapter.notifyDataSetChanged(); } } private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ private TextView mTitleTextView; private TextView mDateTextView; private Crime mCrime; public CrimeHolder(LayoutInflater inflater,ViewGroup parent) { super(inflater.inflate(R.layout.list_item_crime,parent,false)); itemView.setOnClickListener(this); mTitleTextView=(TextView)itemView.findViewById(R.id.crime_title); mDateTextView=(TextView)itemView.findViewById(R.id.crime_date); } public void bind(Crime crime){ mCrime=crime; mTitleTextView.setText(mCrime.getTitle()); mDateTextView.setText((mCrime.getDate().toString())); } @Override public void onClick(View view) { //Intent intent=new Intent(getActivity(),CrimeActivity.class); //Intent intent=CrimeActivity.newIntent(getActivity(),mCrime.getId()); Intent intent=CrimePagerActivity.newIntnet(getActivity(),mCrime.getId()); startActivity(intent); } } private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{ private List<Crime> mCrimes; public CrimeAdapter(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(); } } }
截图如下: