程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> pageradapter-Android 頁卡PagerAdapter相關問題

pageradapter-Android 頁卡PagerAdapter相關問題

編輯:編程綜合問答
Android 頁卡PagerAdapter相關問題

下面是我的代碼

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved