package com.newmap.adapter;import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Environment;import android.support.v7.widget.CardView;import android.support.v7.widget.RecyclerView.Adapter;import android.support.v7.widget.RecyclerView.ViewHolder;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.LinearLayout;import com.newmap.cjapp.DragImageActivity;import com.newmap.cjapp.R;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.util.List;public class PictureAdapter extends Adapter<PictureAdapter.MyViewHolder> { PRivate Context mContext; private List<String> mPictureList; private LayoutInflater inflater; private float density; class MyViewHolder extends ViewHolder { CardView cardView; ImageView iv_picture; public MyViewHolder(View view) { super(view); cardView = (CardView) view.findViewById(R.id.cardView); iv_picture = (ImageView) view.findViewById(R.id.iv_picture); } } public PictureAdapter(Context context, List<String> pictureList) { mContext = context; mPictureList = pictureList; inflater = LayoutInflater.from(mContext); density = mContext.getResources().getDisplayMetrics().density; } @Override public int getItemCount() { return mPictureList.size(); } @Override public void onBindViewHolder(MyViewHolder arg0, final int arg1) { showPicture(arg0.cardView, arg0.iv_picture, mPictureList.get(arg1)); //单个删除在这加arg0.xx获取删除按钮,然后写点击事件,触发删除 } @Override public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) { MyViewHolder holder = new MyViewHolder(inflater.inflate( R.layout.item_picture, arg0, false)); return holder; } private void showPicture(CardView cardView, ImageView imageView, String name) { final String path = Environment.getExternalStorageDirectory().getPath() + "/鄂尔多斯/" + name; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap mBitmap = BitmapFactory.decodeFile(path, options); if (mBitmap == null) {// 调整ImageView的大小 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) cardView .getLayoutParams(); lp.height = (int) (200 * density + 10 * density); double scale = 1.0 * options.outWidth / options.outHeight; lp.width = (int) (200 * scale * density + 10 * density); cardView.setLayoutParams(lp); Log.e("imageview", "宽:" + lp.width + " 高:" + lp.height); } options.inJustDecodeBounds = false; options.inSampleSize = 4; // width,height设为原来的四分之一 InputStream is; try { is = new FileInputStream(path); mBitmap = BitmapFactory.decodeStream(is, null, options); imageView.setImageBitmap(mBitmap); } catch (FileNotFoundException e) { e.printStackTrace(); }//图片的点击删除事件 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(mContext, DragImageActivity.class); intent.putExtra("photopath", path); mContext.startActivity(intent); } }); } public void insertPicture(String name, int position) { mPictureList.add(name); notifyItemInserted(position); } public void insertPicture(String name) { mPictureList.add(name); notifyItemInserted(mPictureList.size() - 1); }//单个删除方法 public void removePicture(int position) { mPictureList.remove(position); notifyItemRemoved(position); }}
新闻热点
疑难解答