Fragment中不能使用自定義帶參構造函數

經過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; }
相關文章
相關標籤/搜索