Android - 在FragmentPagerAdapter中将setArguments分段为片段

时间:2018-02-03 17:35:09

标签: java android android-studio android-fragments fragmentpageradapter

我有一个AppCompatActivity,其上有一个名为TabbedScoreboardFragment的片段,

在该片段中,Tabbar LayoutViewPager,而SectionsPagerAdapterFragmentPagerAdapter的子类。

这是我的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;
    }
}

问题从这里开始:

我正在尝试设置我传递给getItem函数的其中一个片段的参数,但我无法获取片段本身,因为它不是FragmentTransaction它具有replace函数,因此可以使用标记,并且使用我的标记我可以识别片段,获取它并触发函数setArguments(Bundle bundle)

我在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);
        }
    }
}

但是当我在做this.sectionsPagerAdapter.getItem(i)时 它始终创建ScoreboardListFragment的新实例,而不是使用和检索现有实例。

感谢帮助者!

2 个答案:

答案 0 :(得分:0)

当您尝试传递参数时,并非所有内部片段都已创建。 您必须以其他方式传递数据或在创建/显示片段时“询问”数据

答案 1 :(得分:0)

首先,您不应该在get项类型中创建创建片段,因为每次片段l在屏幕上时都会重新创建片段。 你应该做的是用一个片段列表启动适配器,这样你就可以有两个以上的片段,但片段只会初始化一次,所以实例与用setArgs初始化它们的实例相同。