如何使用addOnPageChangerListner refesh Fragment?

时间:2017-06-19 14:42:33

标签: java android android-fragments

您好我使用下面的代码在片段之间切换。 我注意到当我踩它们时片段没有刷新。 如何在每次下载片段时更新我的​​碎片。

anyidea?谢谢

private  void initViewPager()
    {
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        List<Fragment> listFragments = new ArrayList<Fragment>();
        listFragments.add(new FragmentA());
        listFragments.add(new FragmentB());
        //if(fav == true)
        listFragments.add(new FragmentC());

        PagerAdapter myPageAdapter = new PagerAdapter(
                getSupportFragmentManager(),listFragments);
        mViewPager.setAdapter(myPageAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener());


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


    }

    @Override
    public void onPageSelected(int selectedItem)
    {
        mTabHost.setCurrentTab(selectedItem);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onTabChanged(String tabId)
    {
        int selectedItem = mTabHost.getCurrentTab();
        itemTag = mTabHost.getCurrentTabTag();
        mViewPager.setCurrentItem(selectedItem);
        HorizontalScrollView hScrollView = (HorizontalScrollView) findViewById(R.id.h_scroll_view);
        View tabView = mTabHost.getCurrentTabView();
        int scrollPos = tabView.getLeft() - (hScrollView.getWidth()-tabView.getWidth() * 2);
        hScrollView.smoothScrollTo(scrollPos,0);


        }

1 个答案:

答案 0 :(得分:0)

我认为最好创建类BaseFragment extends Fragment,添加自定义void等public void updateFragment()和其他片段等.FragmentA,FragmentB,FragmentC从BaseFragment延伸

public class BaseFragment extends Fragment { 
     public void updateFragment(){
     }
 }

public class FragmentA extends BaseFragment { 
   @Override
   public void updateFragment(){
     //start my job
   }
}

public class BaseFragment extends Fragment { public void updateFragment(){ } } public class FragmentA extends BaseFragment { @Override public void updateFragment(){ //start my job } } 然后在中,您可以更新片段addOnPageChangerListner