今天的任务为创建CrimePagerActivity,代码如下:
CrimePagerActivity.java:
package com.bignerdranch.android.criminallntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import java.util.List; import java.util.UUID; public class CrimePagerActivity extends AppCompatActivity { private static final String EXTRA_CRIME_ID="com.bignerdranch.android.criminalintent.crime_id"; private ViewPager mViewPager; private List<Crime> mCrimes; public static Intent newIntnet(Context packageContext, UUID crimeId){ Intent intent=new Intent(packageContext,CrimePagerActivity.class); intent.putExtra(EXTRA_CRIME_ID,crimeId); return intent; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_crime_pager); UUID crimeId=(UUID)getIntent().getSerializableExtra(EXTRA_CRIME_ID); mViewPager=(ViewPager)findViewById(R.id.crime_view_pager); mCrimes=CrimeLab.get(this).getCrimes(); FragmentManager fragmentManager=getSupportFragmentManager(); mViewPager.setAdapter(new FragmentPagerAdapter(fragmentManager) { @Override public Fragment getItem(int position) { Crime crime=mCrimes.get(position); return CrimeFragment.newInstance(crime.getId()); } @Override public int getCount() { return mCrimes.size(); } }); for(int i=0;i<mCrimes.size();i++){ if(mCrimes.get(i).getId().equals(crimeId)){ mViewPager.setCurrentItem(i); break; } } } }
activity_crime_pager.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/crime_view_pager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager>
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.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 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(); } 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(); } } }
截图如下:
可以左右切换而不卡。