我有一个AppCompatActivity上面有一个片段,叫做TabbedScoreboardFragment

在该片段内有一个Tabbar LayoutViewPager,以及一个SectionsPagerAdapter子类,该子类是FragmentPagerAdapter的子类。

那是我的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的新实例,而不是使用和检索现有实例。

谢谢帮手!

最佳答案

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

10-06 10:09