I can't update the content in ViewPager.
A question: What is the relationship and correct usage of methods instantiateItem() and getItem() in FragmentPagerAdapter class?
I was using only getItem() to instantiate and return my fragments:
@Override
public Fragment getItem(int position) {
return new MyFragment(context, paramters);
}
This worked well (but as said I can't change the content).
So I found this: ViewPager PagerAdapter not updating the View
Particulary one in which it's talked about method instantiateItem():
"My approach is to use the setTag() method for any instantiated view in the instantiateItem() method"
So now I want to implement instantiateItem() in order to do that. But I don't know what I have to return there (return is Object) and what is the relation with getItem(int position)?
Currently I'm using getItem to instantiate the Fragment, is that wrong? But then do I have to put the fragments in instance variable, or not implement getItem() at all...? I just don't understand it.
I tried reading the reference:
public abstract Fragment getItem (int position)
Return the Fragment associated with a specified position.
public Object instantiateItem (ViewGroup container, int position)
Create the page for the given position. The adapter is responsible for adding the view to the container given here, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup). Parameters
container The containing View in which the page will be shown. position The page position to be instantiated.
Returns
Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.
... but I still don't understand how are they related and what I have to do.
Here's my code. I'm using support package v4.
ViewPagerTest
public class ViewPagerTest extends FragmentActivity {
private ViewPager pager;
private MyFragmentAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pager1);
pager = (ViewPager)findViewById(R.id.slider);
String[] data = {"page1", "page2", "page3", "page4", "page5", "page6"};
adapter = new MyFragmentAdapter(getSupportFragmentManager(), 6, this, data);
pager.setAdapter(adapter);
((Button)findViewById(R.id.button)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
reload();
}
});
}
private void reload() {
String[] data = {"changed1", "changed2", "changed3", "changed4", "changed5", "changed6"};
//adapter = new MyFragmentAdapter(getSupportFragmentManager(), 6, this, data);
adapter.setData(data);
adapter.notifyDataSetChanged();
pager.invalidate();
//pager.setCurrentItem(0);
}
}
MyFragmentAdapter
class MyFragmentAdapter extends FragmentPagerAdapter {
private int slideCount;
private Context context;
private String[] data;
public MyFragmentAdapter(FragmentManager fm, int slideCount, Context context, String[] data) {
super(fm);
this.slideCount = slideCount;
this.context = context;
this.data = data;
}
@Override
public Fragment getItem(int position) {
return new MyFragment(data[position], context);
}
@Override
public int getCount() {
return slideCount;
}
public void setData(String[] data) {
this.data = data;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
MyFragment
public final class MyFragment extends Fragment {
private String text;
public MyFragment(String text, Context context) {
this.text = text;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.slide, null);
((TextView)view.findViewById(R.id.text)).setText(text);
return view;
}
}
Here is also somebody with a similar problem, no answers...
http://www.mail-archive.com/android-developers@googlegroups.com/msg200477.html
Use FragmentStatePagerAdapter instead of FragmentPagerAdapter if you want to recreate or reload fragment on index basis For example if you want to reload fragment other than FirstFragment, you can check instance and return position like this
I have lived same problem and I have searched too much times. Any answer given in stackoverflow or via google was not solution for my problem. My problem was easy. I have a list, I show this list with viewpager. When I add a new element to head of the list and I refresh the viewpager nothings changed. My final solution was very easy anybody can use. When a new element added to list and want to refresh the list. Firstly set viewpager adapter to null then recreate the adapter and set i to it to viewpager.
Be sure your adapter must extend FragmentStatePagerAdapter
I slightly modified the solution provided by Bill Phillips to suit my needs
so that the
getItemPosition()
returnsPOSITION_NONE
only for those fragments which are currently in theFragmentManager
whengetItemPosition
is called. (Note that thisFragmentStatePager
and theViewPager
associated with it are contained in a Fragment not in a Activity)I know am late for the Party. I've fixed the problem by calling
TabLayout#setupWithViewPager(myViewPager);
just afterFragmentPagerAdapter#notifyDataSetChanged();
for those who still face the same problem which i faced before when i have a ViewPager with 7 fragments. the default for these fragments to load the english content from api service but the problem here that i want to change the language from settings activity and after finish settings activity i want ViewPager in main activity to refresh the fragments to match the language selection from the user and load the Arabic content if user choosed Arabic here what i did to work from the first time
1- You must use FragmentStatePagerAdapter as mentioned above.
2-on mainActivity i override the onResume and did the following
3-i overrided the getItemPosition() in mPagerAdapter and make it return POSITION_NONE.
works like charm
I use EventBus library to update
Fragment
content inViewPager
. The logic is simple, just like document of EventBus how to do. It is no need to controlFragmentPagerAdapter
instance. The code is here:1: Define events
Define which message which is needed to update.
2.Prepare subscribers
Write below code in the Fragment which is needed update.
3.Post events
Write below code in other
Activity
or otherFragment
which needs to update parameter