解决了(我只需要从FragmentPagerAdapter
更改为FragmentStatePagerAdapter
因为最后一个保存了屏幕中不可见的片段的状态。所以当你需要很多片段时,它们是不可见,内存,信息可能泄漏,活动可能被破坏。这就是GridView发生的事情(下面的问题)添加FragmentStatePagerAdapter
会在片段再次可见时保存片段的状态。
我正在开发一个在线购物应用程序。由于我必须展示大量产品,我使用了大量的分类。我使用的主要功能之一是带有PageAdapter的ViewPager。在每个片段中,它应该是不同的布局。在其中一个片段中,我使用了GridView。问题是,当我在页面之间切换时,我返回到GridView大小的第一页,内部元素的数量不断增加。例如,如果我在开始时有12个元素,在我切换页面然后再次返回后它会变为24,36等。
以下是我用于此的代码: 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;
}
}
答案 0 :(得分:0)
解决了(我只需要从FragmentPagerAdapter
更改为FragmentStatePagerAdapter
因为最后一个保存了屏幕中不可见的片段的状态。所以当你需要很多片段时,它们是不可见,内存,信息可能泄漏,活动可能会被破坏。这就是GridView
发生的事情。添加FragmentStatePagerAdapter
会在片段再次可见时保存片段的状态。
所以只需改变这一行:
public class EbuyFragmentPagerAdapter extends FragmentPagerAdapter
到
public class EbuyFragmentPagerAdapter extends FragmentStatePagerAdapter