I'm trying to make a Level selector to my game, now it's working but, it's really slow on some phones and some times not working (FC), I realized that it's using too much heap memory.
I'm quite new on android dev, so here's my code, I hope you can help me.
ACTIVITY WITH THE VIEWPAGER:
setContentView(R.layout.level_selector);
ViewPagerAdapter adapter = new ViewPagerAdapter(this);
ViewPager myPager = (ViewPager) findViewById(R.id.mypagerwontwork); //cool id huh
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
VIEW PAGER ADAPTER:
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
if(position == 0)
{
View layout = inflater.inflate(R.layout.world1, null);
final GridView gridview = (GridView) layout.findViewById(R.id.gridLvl);
gridview.setAdapter(new LevelAdapter(activity,"World1"));
gridview.setOnItemClickListener(itemClickListener);
((ViewPager) collection).addView(layout);
return layout;
}
GRID VIEW ADAPTER:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
if (mWorld.equals("World1"))
{
imageView.setImageResource(World1[position]);
}
USEFUL INFORMATION:
- There are 3 "Worlds" (View page) each with a different background
- Each "World" has 15 Images, with number from 1 to 15.
- The image size is: +- 12kb
- The BG size is: +- 140kb
I hope it's clear.
thanks.