简体   繁体   English

如何在viewpager中使用不同的参数创建相同的片段

[英]how to create same fragment with different arguments in viewpager

I want to create same fragment multiple times in view pager with different arguments how can i do it? 我想用不同的参数在视图分页器中多次创建相同的片段,我该怎么做?

Now i have a static single argument constructor in a fragment which i call to initialize the fragment 现在我在一个片段中有一个静态单参数构造函数,我调用它来初始化该片段

Is there a simple way to do this. 有没有简单的方法可以做到这一点。

You could use setArguments() method, for each of your Fragment. 您可以对每个Fragment使用setArguments()方法。 Do something like this: 做这样的事情:

Fragment myFragment = new Fragment();
Bundle data = new Bundle();
data.putString("data_1","Hello");
myFragment.setArguments(data);

Then in your Fragment, do this: 然后在您的片段中,执行以下操作:

Bundle data = getArguments();
String data_1 = data.getString("data_1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM