Activity向Fragment傳值,Bundle
阿新 • • 發佈:2018-12-19
首先在Actviity:
public class BusinessMessageActivity extends TitleFragmentActivity {
private List<Fragment> fragments;
if (!ListUtil.isNullOrEmpty(subList)) { fragments = new ArrayList<>(); for (int i = 0; i < subList.size(); i++) { if (!TextUtils.isEmpty(subList.get(i).getExternalLink())) { titles.add(subList.get(i).getName()); fragments.add(HtmlWebFragment.newInstance(subList.get(i).getExternalLink())); } else { titles.add(subList.get(i).getName()); fragments.add(BusinessMessageFragment.newInstance(subList.get(i).getColumnId(), type, subList.get(0).getColumnId())); } } } mAdapter = new TabLayoutListAdapter(getSupportFragmentManager(), fragments, titles); viewPager.setAdapter(mAdapter); viewPager.setCurrentItem(location); tabLayout.setupWithViewPager(viewPager);
接下來是Fragment:
public class BusinessMessageFragment extends BaseFragment { private int key;//快訊型別 private int type = 1;//列表展示形式,1代表小列表,2代表大列表 private int firstId;
public static BusinessMessageFragment newInstance(int key, int type, int firstId) { BusinessMessageFragment fragment = new BusinessMessageFragment(); Bundle bundle = new Bundle(); bundle.putInt("key", key); bundle.putInt("type", type); bundle.putInt("firstId", firstId); fragment.setArguments(bundle); return fragment; }
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_business_message, container, false); unbinder = ButterKnife.bind(this, rootView); if (getArguments() != null) { key = getArguments().getInt("key"); type = getArguments().getInt("type"); firstId = getArguments().getInt("firstId"); }
這就是Bundle傳值。簡單實用