I've tried to search inside SO before post but i'm not able to understand how to refresh the content of ViewPager hosted inside a fragment. Can you help me please?
Thank you for your time
Here my code. I've the fragment activity that host the ViewPager inside it. It show different widget and the ViewPager show different image, i call the returned list of image from the db "carosello" in the code. When i perform a search i call the refresh() method as a callback from the main activity that host this fragment and i setup again all views. Works fine for all the informations in the fragment, but do not works for the ViewPager: the oldest (first time created) image are always showed.
public class Page2FamigliaFragment extends Fragment {
//... variable declaration
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.famiglia_page2,container,false);
//retrieve my object from the main view that host this fragment
info = ((MainFamiglia)this.getActivity()).getMyObject();
//run the setup of the view.
setUpMyPage2View(view);
return view;
}
public void setUpMyPage2View(View view)
{
//... inizialization of some other objects and view - i do not show only because is out of topic for this matter
//retrive the image used in the page viewer
carosello = info.getCarosello();
parts = carosello.split(",");
//how many page ??
int i = parts.length / 3;
int resto = parts.length % 3;
int pages = 0;
if (resto!=0) {
pages = i + 1;
} else {
pages = i;
}
List<Fragment> fragments = new ArrayList<Fragment>();
String[] tempArray;
int startIndex = 0;
int endIndex = 3;
if (parts.length <= 3) {
fragments.add(myCarosello.newInstance(parts));
} else {
tempArray = Arrays.copyOfRange(parts, startIndex, endIndex);
for (int p=0; p < pages; p++) {
fragments.add(myCarosello.newInstance(tempArray));
startIndex = endIndex;
endIndex = ((endIndex+3) > parts.length) ? parts.length : (endIndex+3);
tempArray = Arrays.copyOfRange(parts, startIndex, endIndex);
}
}
pageAdapter = new MyPageAdapter(getChildFragmentManager(), fragments);
pager = (ViewPager) view.findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
pager.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(
event.getAction() == MotionEvent.ACTION_DOWN &&
v instanceof ViewGroup
) {
((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
}
//i call this method from a callback from the main view for refresh this fragment
public void refresh(){
//recupero oggetto dalla activity principale
info = ((MainFamiglia)this.getActivity()).getMyObject();
pager.getAdapter().notifyDataSetChanged();
pager.removeAllViews();
setUpMyPage2View(this.getView());
}
}
This is the class for the pageAdapter
public class MyPageAdapter extends FragmentPagerAdapter {
// fragments to instantiate in the viewpager
private List<Fragment> fragments;
// constructor
public MyPageAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
// return access to fragment from position, required override
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
// number of fragments in list, required override
@Override
public int getCount() {
return this.fragments.size();
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
and this is the class for the imageview inside the pageviewer
public class myCarosello extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
private int numeroImmagini = 0;
public static final myCarosello newInstance(String[] pictures)
{
myCarosello f = new myCarosello();
Bundle bdl = new Bundle(1);
bdl.putStringArray(EXTRA_MESSAGE, pictures);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
String[] pictures = getArguments().getStringArray(EXTRA_MESSAGE);
numeroImmagini = pictures.length;
View view = (LinearLayout)inflater.inflate(R.layout.carosello,container,false);
ImageView imageView1 = (ImageView) view.findViewById(R.id.carosello1);
ImageView imageView2 = (ImageView) view.findViewById(R.id.carosello2);
ImageView imageView3 = (ImageView) view.findViewById(R.id.carosello3);
switch (numeroImmagini) {
case 1:
imageView1.setImageResource(returnImageResource(pictures[0]));
imageView2.setVisibility(View.GONE);
imageView3.setVisibility(View.GONE);
break;
case 2:
imageView1.setImageResource(returnImageResource(pictures[0]));
imageView2.setImageResource(returnImageResource(pictures[1]));
imageView3.setVisibility(View.GONE);
break;
case 3:
imageView1.setImageResource(returnImageResource(pictures[0]));
imageView2.setImageResource(returnImageResource(pictures[1]));
imageView3.setImageResource(returnImageResource(pictures[2]));
break;
}
return view;
}
//return the correct resource name
private int returnImageResource(String image)
{
image = image.replace(" ", "_");
String filenameArray[] = image.split("\\.");
image = "img_" + filenameArray[0].toLowerCase();
int drawableImageId = getActivity().getResources().getIdentifier(image, "drawable", getActivity().getPackageName());
return drawableImageId;
}
}
OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.
1-create a fragment based on the data received by the main activity. 2-change the data of the fragment everytime in the main activity happens something like setFragment(...) with new data (generated always by the main).
You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.
BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, it means offset fragments will be executed on onStart() and the current fragment will be executed onStart() and onResume().
I've solved thanks to this post
changing MyPageAdapter extends FragmentPagerAdapter
to MyPageAdapter extends FragmentStatePagerAdapter
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With