在(二)中我们传递数据的时候新建了一个Fragment对象。然后再利用index把position传给onCreateView();在(三)中我们利用参数,把position传给onCreateView();
修改MyContentFragment
package com.tz.katefragement_repalce;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyContentFragment extends Fragment {
String[] days = new String[] { "今天是星期一", "今天是星期二", "今天是星期三", "今天是星期四",
"今天是星期五", "今天是星期六", "今天是星期日", };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle=this.getArguments();
int position=bundle.getInt("position");
TextView myText=new TextView(getActivity());
myText.setText(days[position]);
return myText;
}
public static MyContentFragment getInstance(int position) {
MyContentFragment fragment = new MyContentFragment();
Bundle bundle=new Bundle();
bundle.putInt("position", position);
fragment.setArguments(bundle);
return fragment;
}
}
关于Fragment为什么使用setArguments()传递参数可参照这篇文章
http://www.tuicool.com/articles/j22E3u