您的位置:首页 > 其它

王学岗Fragment(三)——带参数

2016-01-27 21:13 183 查看
在(二)中我们传递数据的时候新建了一个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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Fragment