第三方框架最新技术Android控件使用篇

使用ARouter实现Activity向Fragment中传值

2021-05-06  本文已影响0人  千夜零一

一般来说,Activity需要向Fragment中传值过去的时候,我们首先会想到Bundle对象,它的一些使用我们都很熟悉:

FirstFragment fragment = new FirstFragment();
Bundle bundle = new Bundle();
bundle.putString("data","传递到的数据");
fragment.setArguments(bundle);//数据传递到fragment中
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.framelayout,fragment);
fragmentTransaction.commit();

关键代码:

FirstFragment fragment = new FirstFragment();
Bundle bundle = new Bundle();
bundle.putString("data","传递到的数据");
fragment.setArguments(bundle);//数据传递到fragment中

主要过程:

在Activity中创建一个Bundle对象,通过bundle的put方法将数据存储,然后通过fragment的setArguments方法,传递Bundle对象到fragment中,然后再在该Fragment中通过getArguments方法得到Bundle数据源,最后通过bundle的get方法,获得指定的数据类型对象。

Bundle bundle =this.getArguments();//得到从Activity传来的数据
String mess = bundle.getString("data");

今天来点不一样的,使用一个我们常用的阿里的ARouter来进行Activity向Fragment的传值

具体代码实现如下:

 val fragmentOne = ARouter.getInstance().build("/kc/fragmentOne")
            .withString("oneStr","$baseStr 首页")
            .navigation() as Fragment
Activity代码:
class Kotlin19 : AppCompatActivity() {
    val vBinding by lazy {
        ActivityKotlin19Binding.inflate(layoutInflater)
    }
    val fragments = ArrayList<Fragment>()
    val titles = ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(vBinding.root)
        setTabLayout()
    }

    //设置TabLayout和ViewPager
    fun setTabLayout(){
        setTitleData()
        titles.forEach {
            vBinding.tabLayout.addTab(vBinding.tabLayout.newTab().setText(it))
        }
        val baseStr = "Activity传递的数据:"
        val fragmentOne = ARouter.getInstance().build("/kc/fragmentOne")
            .withString("oneStr","$baseStr 首页")
            .navigation() as Fragment
        val fragmentTwo = ARouter.getInstance().build("/kc/fragmentTwo")
            .withString("twoStr","$baseStr 看点")
            .navigation() as Fragment
        val fragmentThree = ARouter.getInstance().build("/kc/fragmentThree")
            .withString("threeStr","$baseStr 动态")
            .navigation() as Fragment
        fragments.add(fragmentOne)
        fragments.add(fragmentTwo)
        fragments.add(fragmentThree)
        vBinding.viewPager.adapter = PublicTabAdapter(supportFragmentManager,fragments,titles)
        vBinding.tabLayout.setupWithViewPager(vBinding.viewPager)
    }

    //设置tabTitles数据
    fun setTitleData(){
        titles.add("首页")
        titles.add("看点")
        titles.add("动态")
    }
}

Fragment中代码设置路径和加载数据方法

@Route(path = "/kc/fragmentOne")
class FragmentOne : BaseFragment() {

    @JvmField
    @Autowired
    var oneStr: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_5, container, false)
    }

    override fun initData() {
        super.initData()
        toast(oneStr.toString())
    }
}

其他三个同理。

另外BaseFragment的代码及PublicTabAdapter代码,在我的这个博客:实现Fragment懒加载(结合TabLayout+ViewPager)

上一篇下一篇

猜你喜欢

热点阅读