下面是我的代碼
package photocollage.adapter;
import photocollage.activity.CompileActivity;
import photocollage.activity.MyApplication;
import photocollage.activity.fragment.SceneListener;
import photocollage.activity.fragment.SceneListener.OnSelectSceneListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import com.tools.photoglass.R;
/**
* ViewPager頁卡切換適配器
* @author Administrator
*
*/
public class ViewPagerAdapter2 extends PagerAdapter {
private Context context;
private LayoutInflater inflater;
private int[] imageList;
private OnSelectSceneListener selectListener;
public ViewPagerAdapter2(Context context, int[] array) {
super();
this.context = context;
this.inflater = LayoutInflater.from(context);
this.imageList = array;
}
@Override
public int getCount() {
if (null != imageList) {
return imageList.length;
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
if (view == object) {
return true;
}
return false;
}
@Override
public Object instantiateItem(View container, final int position) {
View v = inflater.inflate(R.layout.activity_scene_page, null);
ImageView image = (ImageView) v.findViewById(R.id.scene_img);
image.setBackgroundResource(imageList[position]);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 傳遞接口的值
selectListener = SceneListener.getInstance().getOnSelectListener();
if (selectListener != null) {
selectListener.onSelectScene(true);
}
MyApplication.isScene = true;
MyApplication.saveCompileType(MyApplication.SCENE);// 保存模板類型
MyApplication.saveCompileId(position + 1);// 保存模板id
Intent intent = new Intent(context, CompileActivity.class);
context.startActivity(intent);
((Activity) context).overridePendingTransition(R.anim.right_in, R.anim.left_out);
}
});
((ViewPager) container).addView(v);
return v;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
}
頁卡view就只顯示一張圖片,但是每次滑動幾下就經常拋出下面這種異常
E/dalvikvm-heap(24885): Out of memory on a 22118416-byte allocation.
這是什麼原因啊?怎麼處理呢?
那個getcount()return改一下試試,改成imagelist.length%10000