从另一个片段中查找制表符位置

时间:2018-06-14 18:00:54

标签: android android-viewpager android-tabs

在我的fragment_whatsHappening中,我有一个标签式viewPagerAdapter,它显示一周中的几天。 (今天,周二,周三,周四,周五,周六,周日) 每个选项卡都会膨胀fragment_schedules_view,其中包含一个recyclelerView,显示当天要执行的任务。 fragment_schedules_view.class包含recyclerView

    ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.schedulesPager);
    SchedulesViewPagerAdapter tabAdapter = new SchedulesViewPagerAdapter(getChildFragmentManager());

    // Add Fragments to adapter one by one
    tabAdapter.addFragment(new fragment_schedules_view(), "Today");
    tabAdapter.addFragment(new fragment_schedules_view(), "Tue");
    tabAdapter.addFragment(new fragment_schedules_view(), "Wed");
    tabAdapter.addFragment(new fragment_schedules_view(), "Thu");
    tabAdapter.addFragment(new fragment_schedules_view(), "Fri");
    tabAdapter.addFragment(new fragment_schedules_view(), "Sat");
    tabAdapter.addFragment(new fragment_schedules_view(), "Sun");
    viewPager.setAdapter(tabAdapter);

    TabLayout tabLayout = (TabLayout)rootView.findViewById(R.id.scheduleTabs);
    tabLayout.setupWithViewPager(viewPager);

在我的fragment_schedules_view.class中有没有办法从fragment_whatsHappening.class获取tabAdapter位置?

2 个答案:

答案 0 :(得分:1)

ViewPager getCurrentItem中有一个返回int的方法,即当前查看页面的位置。

答案 1 :(得分:0)

使用OnTabSelectedListener并使用片段中的接口在每次标签位置发生变化时更新索引。

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
        //reference your method from your other fragment. In my case the 
       // onTabSchedulesChanged method from my fragment_schedules_view.class



        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
});