1、新建super
//super提供统一的FragmentActivity入口。 public abstract class SuperFragmentActivity extends FragmentActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragmentlistactivity); FragmentManager fm=getSupportFragmentManager(); Fragment fragment=fm.findFragmentById(R.id.mainfragment); if (fragment == null){ fragment=createFragment(); fm.beginTransaction().add(R.id.mainfragment,fragment).commit(); } } public abstract Fragment createFragment(); } //接上一个项目,创建好列表 //FragmentListActivity
public class FragmentListActivity extends SuperFragmentActivity{ @Override public Fragment createFragment() {//实现super中的方法 return new StudentListFragment(); } } //创建ListFragment
public class StudentListFragment extends Fragment implements View.OnClickListener { private TextView mNameTextView; private TextView mAgeTextView; private ListmStudents; private Student mStudent; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); View view=inflater.inflate(R.layout.recyclistview, container,false); RecyclerView recyclerView= (RecyclerView) view.findViewById(R.id.recycleview);//Fragment绑定RecycleView recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setAdapter(new RecycleAdpter()); return view; } @Override public void onClick(View v) { startActivity(viewpageActivity.newIntent(getActivity(),mStudent.getNum()));//当被点击时启动启动另一个FragmentActivity } private class RecycleAdpter extends RecyclerView.Adapter { @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater=LayoutInflater.from(getActivity()); View v=inflater.inflate(R.layout.studentview,parent,false); return new RecyclerViewHolder(v); } @Override public void onBindViewHolder(RecyclerViewHolder holder, int position) { mStudent=mStudents.get(position); mNameTextView.setText(mStudent.getName()); mAgeTextView.setText(mStudent.getAge()+""); } @Override public int getItemCount() { mStudents=StudentList.get(); return mStudents.size(); } } private class RecyclerViewHolder extends RecyclerView.ViewHolder{ public RecyclerViewHolder(View itemView) { super(itemView); mAgeTextView= (TextView) itemView.findViewById(R.id.age_textview); mNameTextView= (TextView) itemView.findViewById(R.id.name_textview); itemView.setOnClickListener(StudentListFragment.this); } } }
2、FragmentActivity绑定ViewPage
public class ViewpageActivity extends FragmentActivity { private static final String EXTRA_INDEX = "com.example.ting.criminalintentpractise.viewpage"; private ViewPager mViewPager; private ListmStudentList; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_viewpage); mViewPager = (ViewPager) findViewById(R.id.viewpage); FragmentManager fm = getSupportFragmentManager(); int num = getIntent().getIntExtra(EXTRA_INDEX, 0); mStudentList=StudentList.get(); mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {//将样式文件中的viewpager绑定到一个fragment @Override //滚动的位置 public Fragment getItem(int position) { return StudentFragment.newInstance(position); } @Override //总的滚动页数 public int getCount() { return StudentList.get().size(); } }); mViewPager.setCurrentItem(num); //设置滚动起始页 } public static Intent newIntent(Context context, int index) { Intent intent = new Intent(context, ViewpageActivity.class); intent.putExtra(EXTRA_INDEX, index); return intent; } } 3、新建Fragment
public class StudentFragment extends Fragment { private TextView mNameTextView; private TextView mAgeTextView; private static String ARG_INDEX = "com.example.ting.criminalintentpractise.student"; private int num; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); View v = inflater.inflate(R.layout.studentview, container, false);//启动自身的样式文件 mNameTextView = (TextView) v.findViewById(R.id.name_textview); mAgeTextView = (TextView) v.findViewById(R.id.age_textview); Listlist = StudentList.get(); mNameTextView.setText(list.get(num).getName()); mAgeTextView.setText(list.get(num).getAge()+""); return v; } //为启动该Fragment封装启动参数 public static StudentFragment newInstance(int index) { Bundle bundle = new Bundle(); bundle.putInt(ARG_INDEX, index); StudentFragment fragment = new StudentFragment(); fragment.setArguments(bundle);//通过Arguments将参数传入Fragment return fragment; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); num = getArguments().getInt(ARG_INDEX); //接收Activity传入的参数 } }
4、心得 Intent和Arguments和Bundle关系 1、Activity间相互传质使用Intent。 2、Activity和Fragment间传值使用Arguments。 3、Intent和Arguments传值都是封装在Bundle中进行的,Bundle是Intent和Argument传值的载体。