博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Activity、Fragment、ViewPage
阅读量:6276 次
发布时间:2019-06-22

本文共 5921 字,大约阅读时间需要 19 分钟。

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 List
mStudents; 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 List
mStudentList; @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); List
list = 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传值的载体。

转载于:https://www.cnblogs.com/swordyt/p/6594793.html

你可能感兴趣的文章
MariaDB/Mysql 批量插入 批量更新
查看>>
前端开发工程师必备技能
查看>>
@RequestMapping注解中的url
查看>>
Spring
查看>>
Java基本数据类型学习笔记
查看>>
Django的全文检索
查看>>
ItelliJ IDEA开发工具使用—创建一个web项目
查看>>
搞V6记录
查看>>
OC与js交互获取webview元素和赋值
查看>>
分布式锁的实现
查看>>
ctags --- 每天一个 Linux 命令
查看>>
页面加载新的东西到页面底部
查看>>
java执行命令行,路径有空格的解决办法
查看>>
test
查看>>
學習 React.js:用 Node 和 React.js 創建一個實時的 Twitter 流
查看>>
solr-4.10.4部署到tomcat6
查看>>
切片键(Shard Keys)
查看>>
淘宝API-类目
查看>>
virtualbox 笔记
查看>>
redis操作数据-sets
查看>>