问题
Following this question:
How to recreate all fragments in ViewPager:
I have those classes:
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private List<Fragment> fragments;
/**
* @param fm
* @param fragments
*/
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
and a FragmentActivity
:
public class TabsViewPagerFragmentActivity extends FragmentActivity implements
ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener
{
static final String TAG = TabsViewPagerFragmentActivity.class.getSimpleName();
private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabsViewPagerFragmentActivity.TabInfo>();
public ViewPagerAdapter mPagerAdapter;
.....
Now in this FragmentActivity
I have this listener:
private class SlicerSelectedOnClickListener implements OnClickListener
{
private ODSharedSlicer slicer;
private int slicerIndex;
public SlicerSelectedOnClickListener(ODSharedSlicer slicer, int slicerIntex)
{
super();
this.slicer = slicer;
this.slicerIndex = slicerIntex;
}
public void onClick(View v)
{
slicerOnClickConfiguration(slicerIndex, v);
mPagerAdapter.notifyDataSetChanged();
}
}
....
And this works great notifyDataSetChanged()
is getting called and the ViewPager's Fragments are getting recreated as I want.
The Problem: When I try to call the notifyDataSetChanged()
method of the adapter from another class (In my case it's a class that responsible to parse data that is received from the server) the fragments remain unchanged.
I tried to do this: I created those methods in my FragmentActivity
class (the one with the ViewPager
):
public void NotifyTabActivityViewPagerAdapter()
{
mPagerAdapter.notifyDataSetChanged();
}
public ViewPagerAdapter getTabActivityViewPagerAdapter()
{
return mPagerAdapter;
}
public ViewPager getTabActivityViewPager()
{
return mViewPager;
}
And tried the following from the second class:
((TabsViewPagerFragmentActivity)currentActivity).NotifyTabActivityViewPagerAdapter();
Or:
((TabsViewPagerFragmentActivity)currentActivity).getTabActivityViewPager().setAdapter(((TabsViewPagerFragmentActivity)currentActivity).getTabActivityViewPagerAdapter());
But non of this works. Strangely when I run this:
((TabsViewPagerFragmentActivity)currentActivity).getTabActivityViewPager().setAdapter(null);
The fragments do get removed, But I can't recreate the new set of fragments with the new data received from server side.
What am I missing here? How should I recreate all the fragment in this FragmentActivity
from another class?
Any assistance would be appreciated.
回答1:
There's really like 7 questions in there. But I understand the main question to be updating or sending data to a FragmentActivity
associated with a ViewPager
from some other class. I don't know this other classes relationship to the FragmentActivity
or ViewPager
but there are 2 ways I know of that could handle updating or transfering information the way you want
(1) an interface
If the class you're referring to is another Fragment
and loaded in the same ViewPager
the way you would update the data (or call a method) in another Fragment
or FragmentActivity
is with an interface. You can scroll to the bottom of that link for some sample code but basicly you create a bridge between the Fragment
and the activity that contains the ViewPager
(i.e. the fragment's host activity). You then create a "listener" that looks for some change in the Fragment
, upon which it passes some information to the hoast activity. The host activity then calls a public method within the second Fragment
you need to update. Only tricky part here is to remember to initialize your interface "listener"within your Fragment
's onAttach
method or it won't work.
(2) an intent
If you're dealing with two seperate activities just use an Intent
. Pass an intent to the FragmentActivity
you're looking for and override onNewIntent(Intent i)
to perform whatever function you want within the recieving class.
Hope that helps - I'd need more specificy to answer futher because there's a lot of sub-questions in there.
回答2:
I don't think this is the case and I think that you should make sure that NotifyTabActivityViewPagerAdapter
is running at onPostExecute, but I had a project where
notifyDataSetChange
didn't work and the fragment remained empty.
To solve it, I override the getItemPosition in the adapter as follows:
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
By returning POSITION_NONE
you make sure that the adapter refresh all its data.
来源:https://stackoverflow.com/questions/16794210/update-viewpagers-fragments-from-another-class