通过Fragment自定义的静态方法将值从activity传到fragment中,然后就想到这样不是多次一举吗,为什么不直接写个带参构造函数将值传过去呢?试了一下,发现Fragment有参构造函数竟然报错,抱歉写了这么长时间代码对Fragment还不是很会用,笑哭,感觉任重道远啊...android中Fragment的构造函数
String[] titles = { GlobalConfig.CATEGORY_NAME_APP, GlobalConfig.CATEGORY_NAME_ANDROID, GlobalConfig.CATEGORY_NAME_IOS, GlobalConfig.CATEGORY_NAME_FRONT_END, GlobalConfig.CATEGORY_NAME_RECOMMEND, GlobalConfig.CATEGORY_NAME_RESOURCE}; CommonViewPagerAdapter infoPagerAdapter = new CommonViewPagerAdapter(getSupportFragmentManager(),titles); // App CategoryFragment appFragment = CategoryFragment.newInstance(titles[0]); // Android CategoryFragment androidFragment = CategoryFragment.newInstance(titles[1]); // iOS CategoryFragment iOSFragment = CategoryFragment.newInstance(titles[2]); // 前端 CategoryFragment frontFragment = CategoryFragment.newInstance(titles[3]); // 瞎推荐 CategoryFragment referenceFragment = CategoryFragment.newInstance(titles[4]); // 拓展资源s CategoryFragment resFragment = CategoryFragment.newInstance(titles[5]); infoPagerAdapter.addFragment(appFragment); infoPagerAdapter.addFragment(androidFragment); infoPagerAdapter.addFragment(iOSFragment); infoPagerAdapter.addFragment(frontFragment); infoPagerAdapter.addFragment(referenceFragment); infoPagerAdapter.addFragment(resFragment);
public static CategoryFragment newInstance(String mCategoryName) { CategoryFragment categoryFragment = new CategoryFragment(); Bundle bundle = new Bundle(); bundle.putString(CATEGORY_NAME, mCategoryName); categoryFragment.setArguments(bundle); return categoryFragment; }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_blank, container, false); ButterKnife.bind(this, view); //取出传过来的值 Bundle bundle = getArguments(); mTitle = bundle.getString(ARG_TITLE); return view; }
原文地址:https://www.cnblogs.com/ganchuanpu/p/11971341.html
时间: 2024-10-09 23:15:59