[英]Android - setArguments to fragment in FragmentPagerAdapter
I've got an AppCompatActivity
that has a fragment on it called TabbedScoreboardFragment
, 我有一个AppCompatActivity
,上面有一个名为TabbedScoreboardFragment
的片段,
Inside that fragment there is a Tabbar Layout
with ViewPager
, and the class SectionsPagerAdapter
that subclass from FragmentPagerAdapter
. 在该片段内有一个带有ViewPager
的Tabbar Layout
,以及从FragmentPagerAdapter
ViewPager
的类SectionsPagerAdapter
。
That is my SectionsPagerAdapter
那就是我的SectionsPagerAdapter
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private static final String ME_FRAGMENT_TAG = "meFragment";
private static final String FRIENDS_FRAGMENT_TAG = "friendsFragment";
SectionsPagerAdapter(FragmentManager fm) { super(fm); }
@Override
public Fragment getItem(int position) {
if (position == 0) {
ScoreboardListFragment friendScoreboardListFragment = (ScoreboardListFragment) getChildFragmentManager().findFragmentByTag(FRIENDS_FRAGMENT_TAG);
if (friendScoreboardListFragment == null) {
friendScoreboardListFragment = ScoreboardListFragment.newInstance(UserType.friend);
}
return friendScoreboardListFragment;
} else {
ScoreboardListFragment meScoreboardListFragment = (ScoreboardListFragment) getChildFragmentManager().findFragmentByTag(ME_FRAGMENT_TAG);
if (meScoreboardListFragment == null) {
meScoreboardListFragment = ScoreboardListFragment.newInstance(UserType.me);
}
return meScoreboardListFragment;
}
}
@Override
public int getCount() {
return 2;
}
}
The problem starts here: 问题从这里开始:
I'm trying to set the arguments of one of the fragments that I've passed into the getItem
function, but I can't get the fragment itself, cause it isn't FragmentTransaction
that has the replace
function so it could take a tag and with my the tag i could identify the fragment, get it and fire the function setArguments(Bundle bundle)
我正在尝试设置传递给getItem
函数的片段之一的参数,但是我无法获取片段本身,因为它不是具有replace
函数的FragmentTransaction
,所以它可以使用标签和我的标签,我可以识别片段,得到它并触发函数setArguments(Bundle bundle)
What I've tried to do in TabbedScoreboardFragment
is: 我试图在TabbedScoreboardFragment
做的是:
public class TabbedScoreboardFragment extends Fragment {
private static final String ME_FRAGMENT_TAG = "meFragment";
private static final String FRIENDS_FRAGMENT_TAG = "friendsFragment";
private ViewPager viewPager;
private SectionsPagerAdapter sectionsPagerAdapter;
enum UserType {
me, friend
}
public static TabbedScoreboardFragment newInstance() {
return new TabbedScoreboardFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tabbed_scoreboard, container, false);
this.viewPager = view.findViewById(R.id.container);
this.sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
this.viewPager.setAdapter(this.sectionsPagerAdapter);
TabLayout tabLayout = view.findViewById(R.id.tabs);
this.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(this.viewPager));
this.viewPager.setCurrentItem(1);
return view;
}
@Override
public void setArguments(Bundle args) {
ArrayList<MyScores.MyScore> myScores = args.getParcelableArrayList("myScores");
if (myScores != null) {
for (int i = 0; i < this.sectionsPagerAdapter.getCount(); i++) {
if (this.sectionsPagerAdapter.getItem(i) instanceof ScoreboardListFragment) {
ScoreboardListFragment item = (ScoreboardListFragment) this.sectionsPagerAdapter.getItem(i);
Bundle myScoresBundle = new Bundle();
myScoresBundle.putParcelableArrayList("myScores", myScores);
item.setArguments(myScoresBundle);
}
}
} else {
super.setArguments(args);
}
}
}
But when I'm doing this.sectionsPagerAdapter.getItem(i)
it always creating a new instance of ScoreboardListFragment
instead to use and retrieve the existing one. 但是,当我执行this.sectionsPagerAdapter.getItem(i)
它总是创建一个ScoreboardListFragment
的新实例来代替使用和检索现有实例。
Thanks for helpers! 谢谢帮手!
Not all the fragments inside are created when you are trying to pass the arguments. 当您尝试传递参数时,并不是内部的所有片段都被创建。 You will have to pass the data in another way or to "ask" for the data when you are creating/showing the fragment 创建/显示片段时,您将不得不以其他方式传递数据或“询问”数据
First of all you should not create the create the fragments inside the get item type, because you are recreating the fragment every time the fragments l is on screen. 首先,您不应该在get项类型内创建创建片段,因为每次在屏幕上显示片段l时都会重新创建片段。 What you should do is initiate the adapter with a list of fragments, so then you can have more than two fragments but also the fragments would be just initialize once so the instance is the same as when you initialized them with setArgs. 您应该做的是使用片段列表来启动适配器,这样您可以拥有两个以上的片段,但是这些片段只会被初始化一次,因此实例与使用setArgs初始化它们时相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.