Solved( I just needed to change from FragmentPagerAdapter
to FragmentStatePagerAdapter
becuase the last one saves the states of a fragment that is not visible in the screen. So when you have to many fragments, which are not visible, the memory, information may leak and the activity maybe destroyed. This is what happend with the GridView ( Problem below) Adding FragmentStatePagerAdapter
will save the state of the fragment when it becomes visible again.
I am developing an application for on-line shopping. Due to the big number of products that I have to display I have used a lot of categorization. One of the main feature that I am using is ViewPager with PageAdapter. In every fragment it's supposed to be different layouts. In one of the fragments I have used a GridView. The problem is that when I switch between pages, and I return to the first page the size of the GridView , the number of elements inside keeps growing repeating themselves. For example if I have 12 elements at the beginning, after I switch pages and then get back again it becomes 24, 36 etc.
Below is the code I used for this: EbuyHomeScreen.class
public class EbuyHomeScreen extends FragmentActivity {
@SuppressLint("InlinedApi") protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ebuy_home_screen);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
EbuyFragmentPagerAdapter pagerAdapter = new EbuyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
}
EbuyFragmentPagerAdapter
public class EbuyFragmentPagerAdapter extends FragmentStatePagerAdapter{
final int PAGE_COUNT = 3;
/** Constructor of the class */
public EbuyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
EbuyFragmentLatest myFragmentLatest = new EbuyFragmentLatest();
return myFragmentLatest;
case 1:
EbuyFragmentSold myFragmentSold = new EbuyFragmentSold();
return myFragmentSold;
case 2:
EbuyFragmentSponsored myFragmentSponsored = new EbuyFragmentSponsored();
return myFragmentSponsored;
default:
EbuyFragmentLatest myFragmentLatestDefault = new EbuyFragmentLatest();
return myFragmentLatestDefault;
}
}
/** Returns the number of pages */
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
/** code for returning the title*/ }
}
}
EbuyFragmentLatest
public class EbuyFragmentLatest extends Fragment {
final ArrayList<EbuyItem> ebuy_data = new ArrayList<EbuyItem>();
private EbuyItemAdapter customAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.ebuy_home_fragment_latest,
container, false);
JazzyGridView mGrid = (JazzyGridView)v.findViewById(R.id.ebuy_list);
mGrid.setTransitionEffect(JazzyHelper.TILT);
ebuy_data.add(new EbuyItem("http://www.ebuy.al/Images/dsc/8884_400_300.jpg","Fustan Glamour","2,500L [17.9 €]"));
/*
** filing with data continues */
customAdapter = new EbuyItemAdapter(
container.getContext(), ebuy_data);
mGrid.setAdapter(customAdapter);
return v;
}
}
EbuyItemAdapter
public class EbuyItemAdapter extends BaseAdapter {
private ArrayList<EbuyItem> ebuy_data;
private LayoutInflater layoutInflater;
public EbuyItemAdapter(Context context, ArrayList<EbuyItem> ebuy_data) {
this.ebuy_data = ebuy_data;
layoutInflater = LayoutInflater.from(context);
}
public int getCount() {
return ebuy_data.size();
}
public Object getItem(int position) {
return ebuy_data.get(position);
}
public long getItemId(int position) {
return position;
}
@SuppressLint({ "DefaultLocale", "InflateParams" })
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.ebuy_item, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.price = (TextView) convertView.findViewById(R.id.price);
holder.image = (ImageView) convertView
.findViewById(R.id.ebuy_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(((EbuyItem) ebuy_data.get(position)).getName());
holder.price.setText(((EbuyItem) ebuy_data.get(position)).getPrice());
UrlImageViewHelper.setUrlDrawable(holder.image,
((EbuyItem) ebuy_data.get(position)).getImageUrl(),
R.drawable.loading);
return convertView;
}
static class ViewHolder {
TextView name;
TextView price;
ImageView image;
}
}
Solved( I just needed to change from FragmentPagerAdapter
to FragmentStatePagerAdapter
becuase the last one saves the states of a fragment that is not visible in the screen. So when you have to many fragments, which are not visible, the memory, information may leak and the activity maybe destroyed. This is what happend with the GridView
. Adding FragmentStatePagerAdapter
will save the state of the fragment when it becomes visible again.
So just change this line:
public class EbuyFragmentPagerAdapter extends FragmentPagerAdapter
to
public class EbuyFragmentPagerAdapter extends FragmentStatePagerAdapter
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다