Пример #1
0
  // 依附的activity创建完成
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    initData();
  }
Пример #2
0
  @Override
  public void initData() {
    super.initData();
    rgGroup.check(R.id.rb_home); // 默认勾选首页

    mPagerList = new ArrayList<BasePager>();
    /*
     * for (int i = 0; i < 5; i++) { BasePager basePager = new
     * BasePager(mActivity); mPagerList.add(basePager); }
     */
    mPagerList.add(new HomePager(mActivity));
    mPagerList.add(new NewsCenterPager(mActivity));
    mPagerList.add(new SmartServicePager(mActivity));
    mPagerList.add(new GovAffairsPager(mActivity));
    mPagerList.add(new SettingPager(mActivity));

    mViewPager.setAdapter(new ContentAdapter());

    // 监听RadioGroup的选择事件
    rgGroup.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {

          @Override
          public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
              case R.id.rb_home:
                mViewPager.setCurrentItem(0, false);
                break;
              case R.id.rb_news:
                mViewPager.setCurrentItem(1, false);
                break;
              case R.id.rb_smart:
                mViewPager.setCurrentItem(2, false);
                break;
              case R.id.rb_gov:
                mViewPager.setCurrentItem(3, false);
                break;
              case R.id.rb_setting:
                mViewPager.setCurrentItem(4, false);
                break;

              default:
                break;
            }
          }
        });

    mViewPager.addOnPageChangeListener(
        new OnPageChangeListener() {

          @Override
          public void onPageSelected(int position) {
            mPagerList.get(position).initData(); // 获取当前被选中的页面, 初始化该页面数据
          }

          @Override
          public void onPageScrolled(
              int position, float positionOffset, int positionOffsetPixels) {}

          @Override
          public void onPageScrollStateChanged(int state) {}
        });

    mPagerList.get(0).initData(); // 默认初始化首页数据
  }