非常简单一种是通过bundle,另外一种是通过fragment提供的instantiate(过时方法),都是kotlin版本,直接上代码吧:
方法一:
//fragment传递参数方法一
val fragment = MvPagerFragment()
val bundle = Bundle()
bundle.putString("args", list?.get(position)?.name)
fragment.arguments = bundle
方法二:
//fragment传递参数方法二
val bundle = Bundle()
bundle.putString("args", list?.get(position)?.code)
Fragment.instantiate(context, MvPagerFragment::class.java.name, bundle)
完整代码
看下java版本
if (null == takePhotoFragment) {
takePhotoFragment = new TakePhotoFragment();
}
Bundle bundle = new Bundle();
bundle.putString("name", "拍照");
takePhotoFragment.setArguments(bundle);
//方法一传参数
Fragment instantiate = TakePhotoFragment.instantiate(this, TakePhotoFragment.class.getName(), bundle);
//方法二传参数
getSupportFragmentManager().beginTransaction().replace(R.id.ll_replace, instantiate).addToBackStack(this.getClass().getSimpleName()).commit();
// getSupportFragmentManager().beginTransaction().replace(R.id.ll_replace, takePhotoFragment).addToBackStack(this.getClass().getSimpleName()).commit();
Google官方文档:fragment传参方法二官方文档
做个记录