如何刷新viewpager中的片段?

时间:2013-12-10 12:52:38

标签: android android-fragments android-viewpager

我知道这个问题已经存在一些问题。但我在Android中非常新,特别是FragmentsViewpager。请对我充满激情。我找不到符合我代码的答案。

我不知道如何刷新片段或重新加载它再次“活动”。

TabsPagerAdapter.java:

public class TabsPagerAdapter extends FragmentPagerAdapter{


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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:

            return new KFZFragment();
        case 1:

            return new LogFragment();
        case 2:

            return new TrackFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }

}

我有3个片段(KFZFragment,LogFragment,TrackFragment),在TrackFragment我计算了一些数据,这些数据应该显示在LogFragment的ListView中。 但是当我改为LogFragment时,它不是最新的数据。所以它不会刷新。

现在我应该如何修改代码以在片段“活动”时刷新片段?

MainActivityFragment.java:

public class MainActivityFragment extends FragmentActivity implements ActionBar.TabListener{

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    List<Fragment> fragments;

    private String[] tabs = { "KFZ", "Fahrten Log", "Kosten Track" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment);

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

       fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, KFZFragment.class.getName(),savedInstanceState));
        fragments.add(Fragment.instantiate(this, LogFragment.class.getName(),savedInstanceState));

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }   
    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {


        }
        return super.onKeyDown(keyCode, event);
    }

}

请帮助我。

3 个答案:

答案 0 :(得分:2)

在您的KFZFragmentLogFragmentTrackFragment中,您应该能够覆盖片段类中的onResume并将必要的刷新/重新加载代码放在那里

答案 1 :(得分:0)

为什么不呢?使用静态方法刷新数据

答案 2 :(得分:0)

我同意Jonathan Lin

但是如果你要更新的片段是当前显示的片段,你可以做类似的事情:

public class DisplayPagerAdapter extends FragmentPagerAdapter {

private static final String TAG = "DisplayPagerAdapter";

SparseArray<DisplayFragment> registeredFragments = new SparseArray<DisplayFragment>();

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

@Override
public int getCount() {
    DisplayCoreModule display = DisplayCoreModule.getInstance();
    return (display != null && display.getPagesCount() > 0) ? display.getPagesCount() : 1;
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}


@Override
public Fragment getItem(int position) {
    Log.d(TAG, "getItem " + position);
    return DisplayFragment.newInstance(position); 
}

@Override
public CharSequence getPageTitle(int position) {
    DisplayCoreModule display = DisplayCoreModule.getInstance();
    if (display != null && display.getPagesCount() > 0) {
        return "Side " + (position+1);
    } else {
        return super.getPageTitle(position);
    }
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    Log.d(TAG, "instantiateItem " + position);
    DisplayFragment fragment = (DisplayFragment) super.instantiateItem(container, position);
    registeredFragments.put(position, fragment);
    return fragment;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    Log.d(TAG, "destroyItem " + position);
    registeredFragments.remove(position);
    super.destroyItem(container, position, object);
}

public Fragment getRegisteredFragment(int position) {
    return registeredFragments.get(position);
}

public SparseArray<DisplayFragment> getRegisteredFragments() {
    return registeredFragments;
}

}

这是我想向您展示的registeredFragments部分。

由于您使用FragmentPagerAdapter,因此在您提供有效位置的情况下,getRegisteredFragment(int position)将始终返回您的一个片段。如果你在某个时候更改为FragmentStatePagerAdapter,它可以在某些位置返回null。

现在,在TrackFragment中创建一个对MainActivity的回调,然后通知LogFragment,这里以伪代码显示:

Fragment TrackFragment  {

    interface TrackFragmentCallback() {
        public void newInfo()
    }

    TrackFragmentCallback trackFragmentCallback;

    onAttatch() {
        // make sure to implement TrackFragmentCallback on MainActivity 
        trackFragmentCallback = (TrackFragmentCallback)getActivity();
    } 

    // When this fragment has new info, simply do:
    trackFragmentCallback.newInfo();
}

MainActivity implements TrackFragmentCallback {

    newInfo() {
        // get LogFragment
        LogFragment logFragment = pagerAdapter.getRegisteredFragment(1); 
        if (logFragment != null) {
            logFragment.update();
        }
    }
}

希望这有帮助。