滑动不使用我的自定义图库

时间:2016-09-02 07:26:50

标签: android gallery android-glide

我有一个画廊,但它使用了太多的内存。

我想实现Glide,但它不会工作。

我的公共ImageAdapter类:

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Activity context;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() { return count; }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
            holder.checkbox.setId(position);
            holder.imageview.setId(position);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        View.OnClickListener clickListener = new View.OnClickListener() {


            public void onClick(View v) {
                int id = holder.checkbox.getId();

                if (thumbnailsselection[id]) {
                    holder.checkbox.setChecked(false);
                    thumbnailsselection[id] = false;
                } else {
                    holder.checkbox.setChecked(true);
                    thumbnailsselection[id] = true;
                }
            }

        };

        holder.checkbox.setOnClickListener(clickListener);
        holder.imageview.setOnClickListener(clickListener);

        holder.imageview.setOnLongClickListener(new View.OnLongClickListener() {

            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                int id = v.getId();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
                startActivity(intent);
                return true;
            }

        });
        holder.imageview.setImageBitmap(thumbnails[position]);
        holder.checkbox.setChecked(thumbnailsselection[position]);
        holder.id = position;


        Glide.with(context).load(holder.imageview)
                .placeholder(R.drawable.logo).centerCrop()
                .into((ImageView) convertView);

        return convertView;
    }
}

class ViewHolder {
    ImageView imageview;
    CheckBox checkbox;
    int id;
}

oncreate方法:

 GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
 imagegrid.setAdapter(new ImageAdapter());

logcat的:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.Activity.isDestroyed()' on a null object reference
at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:133)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:125)
at com.bumptech.glide.Glide.with(Glide.java:641)
at com.example.example.photonet.Gallery$ImageAdapter.getView(Gallery.java:170)

我必须将convertView转换为Imageview到AndroidStudio接受它。

没有Glide部分,效果很好。所以问题出在那一部分。

3 个答案:

答案 0 :(得分:0)

更改

注意:使用上下文。你需要像Sohail Zahid这样的背景回答

Glide.with(context).load("Url")
            .placeholder(R.drawable.logo).centerCrop()
            .into(holder.imageview);

对于Load sdcard,您需要遵循此

 String fileName = "1.jpg";
 String completePath = Environment.getExternalStorageDirectory() + "/" + fileName;

File file = new File(completePath);
Uri imageUri = Uri.fromFile(file);

Glide.with(this)
        .load(imageUri)
                .into(holder.imageview);

答案 1 :(得分:0)

在设置adatper时,在构造函数context中作为参数进行一些修改,如下所示。

ImageAdapter(context)

imagegrid.setAdapter(new ImageAdapter(ActivityName.this));

更新: public class ImageAdapter extends BaseAdapter { private LayoutInflater mInflater; private Context context; public ImageAdapter(Context context) { mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.context = context; } (ImageView) convertView

holder.imageview

答案 2 :(得分:0)

GalleryImages image = images.get(position);
//image.getMedium() == this is your image url
//holder.thumbnail = this is your imageview
    Glide.with(mContext).load(image.getMedium())

            .thumbnail(0.5f)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(holder.thumbnail);