我需要有关在视图分页器中管理多个片段的指导

时间:2019-03-21 00:43:52

标签: java android android-fragments

我有一个项目在某个地方分叉,由于是Android的新手,所以我确实需要有关如何将数据从视图分页器片段传递到代码中包含的子片段的指导。

我的界面正在实现一些外观,或类似于标签内的标签的工作,正如您在 SectionsFragment.java 文件中看到的那样

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(Objects.requireNonNull(getActivity()).getSupportFragmentManager());
    adapter.addFragment(new SectionsFragmentRecent());
    adapter.addFragment(new SectionsFragmentPopular());
    adapter.addFragment(new SectionsFragmentRelevant());
    viewPager.setAdapter(adapter);
}

这是我的 SectionsFragmentTabs.java ,正在努力修改以从父片段 SectionsFragmentRecent.java 指向的片段之一中达到所需条件。 >

public class SectionsFragmentTabs extends Fragment {

    private View view;
    private RecyclerView mainRecyclerView;

    public SectionsFragmentTabs() { }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.sections_fragment_tabs, container, false);
        configureMainRecyclerView();

        return view;
    }

    private void configureMainRecyclerView() {
        mainRecyclerView = view.findViewById(R.id.questions_recycler_view);
        mainRecyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mainRecyclerView.setLayoutManager(layoutManager);

        loadMainItemsData();
    }

}

所以我想修改 SectionsFragment.java 以这种方式传递数据

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(Objects.requireNonNull(getActivity()).getSupportFragmentManager());
    adapter.addFragment(new SectionsFragmentTabs("recent"));
    adapter.addFragment(new SectionsFragmentTabs("popular"));
    adapter.addFragment(new SectionsFragmentTabs("relevant"));
    viewPager.setAdapter(adapter);
}

但是我似乎看不到如何在我的 SectionsFragmentTabs.java 中创建参数化构造函数来实现这一目标

0 个答案:

没有答案