robert_hly

踩坑填坑记录...

导航

Activity中通过标签获取当前Fragment

初始化完成之后才有数据,否则获取不到

  String tag = "android:switcher:"+viewPager.getId()+":"+viewPager.getCurrentItem();

  Fragment fg = getSupportFragmentManager().findFragmentByTag(tag);

  if (fg == null) {
      return;
  }

  switch (viewPager.getCurrentItem()) {
      case 0:
                ((Fragment1)fg).updateUI();
            break;
      case 1:
              ((Fragment2) fg).upDateUI();
         break;
      case 2:
          ((Fragment3) fg).upDateUI();
          break;

  }

 

//ViewPage滑动变化监听

 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
             //   doSomething
            }

            @Override
            public void onPageSelected(int position) {
                     switch (position){
                      case 0:
                     //   doSomething
                          break;
                      case 1:
                         //   doSomething

                   break;
                      case 2:
                          //   doSomething
                          break;
                  }
              }

            @Override
            public void onPageScrollStateChanged(int state) {
                //完全隐藏或显示时
                if (state == 0) {
                      //   doSomething
                }
             }
     });

 

posted on 2017-03-04 08:56  robert_hly  阅读(368)  评论(0编辑  收藏  举报