2017-07-17 91 views
0

我已經使用ViewPager disapear當片段移動

getChildFragmentManager和CustomPager

但是當我移動片段我的觀點始終尋呼機disapear。

這是我setupViewPager

private void setupViewPager(ViewPager viewPager) { 
    MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager()); 
    adapter.addFragment(new PulsaFragment(), "Pulsa"); 
    adapter.addFragment(new TelkomFragment(), "Telkom"); 
    adapter.addFragment(new BPJSFragment(), "BPJS"); 
    adapter.addFragment(new ListrikFragment(), "Listrik"); 
    viewPager.setAdapter(adapter); 
} 

和myPagerAdapter

class MyPagerAdapter extends FragmentPagerAdapter { 

    private List<Fragment> mFragmentList = new ArrayList<>(); 
    private final List<String> mFragmentTitleList = new ArrayList<>(); 
    private int mCurrentPosition = -1; 

    @Override 
    public void setPrimaryItem(ViewGroup container, int position, Object object) { 
     super.setPrimaryItem(container, position, object); 
     if (position != mCurrentPosition) { 
      Fragment fragment = (Fragment) object; 
      CustomPager pager = (CustomPager) container; 
      if (fragment != null && fragment.getView() != null) { 
       mCurrentPosition = position; 
       pager.measureCurrentView(fragment.getView()); 
      } 
     } 
    } 

    MyPagerAdapter(FragmentManager manager) { 
     super(manager); 
    } 

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

    @Override 
    public int getCount() { 
     return mFragmentList.size(); 
    } 

    void addFragment(Fragment fragment, String title) { 
     mFragmentList.add(fragment); 
     mFragmentTitleList.add(title); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitleList.get(position); 
    } 

} 

請幫我:) 我已經試過this但其沒有工作..

+1

'我移動片段'這是什麼意思?你如何移動它? –

+0

上面的代碼工作正常。 – Bharatesh

回答

0

試試這個

class ViewPagerAdapter extends FragmentPagerAdapter { 
    private final List<Fragment> mFragmentList = new ArrayList<>(); 
    private final List<String> mFragmentTitleList = new ArrayList<>(); 

    public ViewPagerAdapter(FragmentManager manager) { 
     super(manager); 
    } 

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

    @Override 
    public int getCount() { 
     return mFragmentList.size(); 
    } 

    public void addFragment(Fragment fragment, String title) { 
     mFragmentList.add(fragment); 
     mFragmentTitleList.add(title); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitleList.get(position); 
    } 
}