项目遇到,gridview有一个item是点击后选择的,但又新的item加如后这个就不断向后移动
在adapter中将返回的项数加一
if(position<mSelectPath.size()){ //添加list中的数据 }else { image.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.biaoqing)); }
class MyGridAdapter extends BaseAdapter{ private Context mContext; public MyGridAdapter(Context context) { this.mContext=context; } public void setData(){ notifyDataSetChanged(); } @Override public int getCount() { return mSelectPath.size()+1; } @Override public Object getItem(int position) { return mSelectPath.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView image; if (convertView == null) { convertView = View.inflate(mContext, R.layout.gridview_view, null); image = (ImageView) convertView.findViewById(R.id.image_gridview); } else { image = (ImageView) convertView.findViewById(R.id.image_gridview); } if(position<mSelectPath.size()){ String path = mSelectPath.get(position); Bitmap bitmap = convertToBitmap(path, 500, 400); image.setImageBitmap(bitmap); }else { image.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.biaoqing)); } return convertView; }
点击事件:else的时候就是点击的默认的呢一项
mGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { if (position < parent.getCount() - 1) { String path = (String) parent.getAdapter().getItem(position); for (int i = 0; i < mSelectPath.size(); i++) { if (path.equals(mSelectPath.get(i))) { mSelectPath.remove(mSelectPath.get(i)); myGridAdapter.setData(); break; } } } else { Intent intent = new Intent(MyTest.this, MultiImageSelectorActivity.class); // 是否显示拍摄图片 intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, false); // 最大可选择图片数量 intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 9); // 选择模式 intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, 1); // 默认选择 if (mSelectPath != null && mSelectPath.size() > 0) { intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath); } startActivityForResult(intent, REQUEST_IMAGE); } return true; } });
String路径转成Bitmap
//String转换成bitmap public Bitmap convertToBitmap(String path, int w, int h) { BitmapFactory.Options opts = new BitmapFactory.Options(); // 设置为ture只获取图片大小 opts.inJustDecodeBounds = true; opts.inPreferredConfig = Bitmap.Config.ARGB_8888; // 返回为空 BitmapFactory.decodeFile(path, opts); int width = opts.outWidth; int height = opts.outHeight; float scaleWidth = 0.f, scaleHeight = 0.f; if (width > w || height > h) { // 缩放 scaleWidth = ((float) width) / w; scaleHeight = ((float) height) / h; } opts.inJustDecodeBounds = false; float scale = Math.max(scaleWidth, scaleHeight); opts.inSampleSize = (int)scale; WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts)); return Bitmap.createScaledBitmap(weak.get(), w, h, true); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-08 00:56:19