更改用户设置的过滤器时更新viewpager片段

时间:2018-08-05 16:31:21

标签: android android-fragments android-viewpager

我的活动有一个fab,它使用户可以过滤要在每个列表中看到的电影类型。

“ UpcomingFragment”具有方法“ loadReleaseData”,该方法使用片段当月的更新类型过滤器获取数据。相同的“ UpcomingFragment”片段重复14次,每个片段关联一个月。

例如,viewpager中的第一个片段将显示当月(2018年8月)的电影发行,viewpager中的下一个“ UpcomingFragment”将显示下个月(2018年9月)的电影发行。

每当检测到流派​​变化时,我需要更新所有片段内容(将新片段添加到共享首选项或删除旧片段)

这是我的viewpager片段,其中包含所有“ UpcomingFragment”:

public class UpcomingViewPagerFragment extends Fragment {

    public ViewPager mViewPager;

    public UpcomingViewPagerFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_upcoming_view_pager, container, false);
        // Viewpager
        mViewPager = view.findViewById(R.id.pager);
        mViewPager.setOffscreenPageLimit(-1);
        // Get TabLayout from Main
        TabLayout tabLayout = ((MainActivity)getActivity()).mTabLayout;
        tabLayout.setupWithViewPager(mViewPager);

        final SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat monthFormatter = new SimpleDateFormat("MMMM");
        // Add 11 months [Maximum] to show the releases for the next year [14 fragments in total]
        // First page recently released [-1] gets the last month of the current month
        calendar.add(Calendar.MONTH, -1);
        mSectionsPagerAdapter.addFragment(new UpcomingGamesFragment(), monthFormatter.format(calendar.getTime()) + " " + calendar.get(Calendar.YEAR), "recently");
        // Set it back to normal
        calendar.add(Calendar.MONTH, 1);
        for (int i = 0; i < 12; i++) {
            String title = monthFormatter.format(calendar.getTime()) + " " + calendar.get(Calendar.YEAR);
            // m_yyyy
            String queryId = (calendar.get(Calendar.MONTH) + 1) + "_" + calendar.get(Calendar.YEAR);
            mSectionsPagerAdapter.addFragment(new UpcomingGamesFragment(), title, queryId);
            calendar.add(Calendar.MONTH, 1);
        }
        // Last page: Coming in the future, everything after the last 11th month
        mSectionsPagerAdapter.addFragment(new UpcomingGamesFragment(), "Near future", "future");
        mViewPager.setAdapter(mSectionsPagerAdapter);
        // Because the first page will always be the recently released
        mViewPager.setCurrentItem(1, true);
        // Listener to update
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                ((UpcomingGamesFragment) mSectionsPagerAdapter.getCurrentFragment()).loadReleaseData(0);
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}

0 个答案:

没有答案