Android Open Source - NestedFragment First Tab Fragment






From Project

Back to project page NestedFragment.

License

The source code is released under:

GNU General Public License

If you think the Android project NestedFragment listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sams.nestedfragment;
//from w  ww  . j a  va2 s . c  om
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FirstTabFragment extends Fragment {
  
  private ViewPager mViewPager;
  private SectionsPagerAdapter mSectionsPagerAdapter;
  
  private final int PAGE_COUNT = 3;
  private int mViewPagerPosition = 0;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    
    View rootView = inflater.inflate(R.layout.fragment_firsttab, container,false);
    mViewPager = (ViewPager) rootView.findViewById(R.id.vp_pager);
    
    //swipe for different pages
    mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
    mViewPager.setAdapter(mSectionsPagerAdapter);
    
    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
      
      @Override
      public void onPageSelected(int position) {
        mViewPagerPosition  = position;
          
      }
      
      @Override
      public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
        
      }
      
      @Override
      public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
        
      }
    });
    
    mViewPager.setOffscreenPageLimit(PAGE_COUNT);
    return rootView;
  }
  
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      // notifying nested fragments (support library bug fix)
      final FragmentManager childFragmentManager = getChildFragmentManager();
     
      childFragmentManager.getFragments().get(mViewPagerPosition).onActivityResult(requestCode, resultCode, data);
      
     
     /* if (childFragmentManager != null) {
          final List<Fragment> nestedFragments = childFragmentManager.getFragments();
         
          
          if (nestedFragments == null || nestedFragments.size() == 0) return;
   
          for (Fragment childFragment : nestedFragments) {
              if (childFragment != null && !childFragment.isDetached() && !childFragment.isRemoving()) {
                  childFragment.onActivityResult(requestCode, resultCode, data);
              }
          }
      }*/
  }
  
  //adapter for feeder viewpager (pages for each feeder)
  public class SectionsPagerAdapter extends FragmentStatePagerAdapter{

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

    @Override
    public Fragment getItem(int Position) {
      return PagesFragment.getInstance(Position);
    }

    @Override
    public int getCount() {
      return PAGE_COUNT;
    }
    
        
  }

}




Java Source Code List

com.sams.nestedfragment.FirstActivity.java
com.sams.nestedfragment.FirstTabFragment.java
com.sams.nestedfragment.MainActivity.java
com.sams.nestedfragment.PagesFragment.java
com.sams.nestedfragment.SecondActivity.java
com.sams.nestedfragment.SecondTabFragment.java