如何在ViewPager中动态更改片段?

时间:2017-07-01 09:43:33

标签: android android-fragments android-viewpager

我创建了6个片段。前3个片段将在早晨时间在viewPager中加载,其他3个片段将在晚上时间到来。如何根据时间动态更新viewPager与所需的片段集?这是我的MainActivity代码。我在viewPager中实现了前3个片段。但是如何在viewPager中加载其他3个片段?

MainActivity:

mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // adding bottom dots
        addBottomDots(0);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                MyFragmentInterface fragmentInterface = (MyFragmentInterface) mSectionsPagerAdapter.instantiateItem(mViewPager, position);
                if (fragmentInterface != null) {
                    fragmentInterface.fragmentBecomeVisible();
                    Log.e("PageSelected", position + "");
                }
                addBottomDots(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    private class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            switch (position) {
                case 0: {
                    return new Fragment1();
                }
                case 1: {
                    return new Fragment2();
                }
                case 2: {
                    return new Fragment3();
                }
            }

            return null;

        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return totalTabs;
        }
    }

如何在晚上加载其他3个片段?

更新: 照顾解决方案我认为会出现一个问题。如果用户从早上起使用该应用程序该怎么办?片段会在下午12:05实时更新吗?

4 个答案:

答案 0 :(得分:3)

鉴于下一个连续数字是晚间片段,你可以做这样的事情。

switch (position) {
            case 0: {
                if (time != Calendar.AM){
                    return new Fragment4();
                }
                else {
                    return new Fragment1();
                }
            }
            case 1: {
                if (time != Calendar.AM){
                    return new Fragment5();
                }
                else {
                    return new Fragment2();
                }
            }
            case 2: {
                if (time != Calendar.AM){
                    return new Fragment3();
                }
                else {
                    return new Fragment6();
                }
            }
        }

答案 1 :(得分:2)

更新您的代码

switch (position) {
case 0: {
  if(condition){
    return new FragmentOther1();
  }
  return new Fragment1();
}
case 1: {
  if(condition){
    return new FragmentOther2();
  }
  return new Fragment2();
}
case 2: {
 if(condition){
   return new FragmentOther3();
 }
  return new Fragment3();
}

并将您的晚间条件计算为您的时段。

实时添加接收器

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(Intent.Intent.ACTION_TIME_TICK)){
            //notify adapter data from here. and also check time logic. this method will call for every minute.
        }
    }
};

在onCreate

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.Intent.ACTION_TIME_TICK);
registerReceiver(receiver, filter);

并在onDestory

 if (receiver != null) {
  unregisterReceiver(receiver);
  receiver = null;
}

答案 2 :(得分:1)

添加动态片段计数集

@Override
public int getCount() {

    // check calendar session.
    if(morning == true) {
    // Show 3 total pages morning.
        return 3;
    }else{
    // Show 3 + 3  6 total pages evening.
        return 6;
    }
}

然后添加片段

@Override
public Fragment getItem(int position) {
    // getItem is called to instantiate the fragment for the given page.
    // Return a PlaceholderFragment (defined as a static inner class below).
    switch (position) {
        case 0: {
        return new Fragment1 ();
    }
        case 1: {
        return new Fragment2 ();
    }
        case 2: {
        return new Fragment3 ();
    }
        case 3: {
        return new Fragment4 ();
    }
        case 4: {
        return new Fragment5 ();
    }
        case 5: {
        return new Fragment6 ();
    }
    }

    return null;

}

答案 3 :(得分:1)

为什么你必须让它如此复杂?创建2个列表:白天列表和夜晚列表。根据您的情况检查时间setAdapternotifyDataSetChanged()

private class SectionsPagerAdapter extends FragmentStatePagerAdapter {
    private List<Fragment> fragmentList;

    public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        this.fragmentList = fragmentList;
    }

    SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void setFragmentList(List<Fragment> fragmentList) {
        this.fragmentList = fragmentList;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return fragmentList.size();
    }
}

以下是如何实现

    List<Fragment> mDayFragments = new ArrayList<>();
    mDayFragments.add(Fragment1);
    mDayFragments.add(Fragment2);
    mDayFragments.add(Fragment3);

    List<Fragment> mNightFragments = new ArrayList<>();
    mNightFragments.add(Fragment4);
    mNightFragments.add(Fragment5);
    mNightFragments.add(Fragment6);

    SectionsPagerAdapter mAdapter;

    if (yourCondition) {
        mAdapter = new SectionsPagerAdapter(mDayAdapter);
    } else {
        mAdapter = new SectionsPagerAdapter(mNightAdapter);
    }

    yourViewPager.setAdapter(mAdapter);

    // Time moving... The night comes...
    mDayAdapter.setFragmentList(mNightFragments);