Reputation: 575
i have error with tab viewpagerindicator, if if back or swipe to previous fragment is always reload...
this my code fragment
package com.droidersuin.project.viewpagerindicator;
import com.droidersuin.project.app.ContentActivity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewAdapter extends FragmentPagerAdapter{
private Context _context;
String[] page_titles;
public ViewAdapter(Context context, FragmentManager fm, String[] page_title){
super(fm);
_context = context;
this.page_titles = page_title;
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
Fragment f = new Fragment();
switch(position){
case 0:
f = ContentActivity.newInstance(_context);
break;
case 1:
f = ContentActivity.newInstance(_context);
break;
case 2:
f = ContentActivity.newInstance(_context);
break;
}
return f;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return page_titles.length;
}
public CharSequence getPageTitle(int position) {
return page_titles[position];
}
}
if i swipe from fragment 0 to fragment index 1 and back swipe to index 0, is always reload content, i want to make if back swipe to previous fragment not reload again ? how it
thanks... sorry for my english ..
Upvotes: 0
Views: 427
Reputation: 491
I think you shold try to override getItemId Method like that:
@Override
public long getItemId(int position) {
return position;
}
If you check FragmentPagerAdapter's source code you will find it did this in instantiateItem method
final long itemId = getItemId(position);
// Do we already have this fragment?
String name = makeFragmentName(container.getId(), itemId);
Fragment fragment = mFragmentManager.findFragmentByTag(name);
FragmentPagerAdapter uses item id to identify if fragment has already been instantiated.
Upvotes: 0
Reputation: 4860
Try to set your viewPager's offScreenPageLimit like this:
mPager.setOffscreenPageLimit(3);
it will keep your three pages in memory, and after them it will start reloading.
Upvotes: 4