您好,登錄后才能下訂單哦!
本文介紹了Android用RecyclerView實現動態添加本地圖片,分享給大家,具體如下:
本文所用的多圖選擇的library來自:https://github.com/lovetuzitong/MultiImageSelector
簡單介紹一下用法:
1、跳轉到圖片選擇頁面:
Intent intent = new Intent(PassengerSetActivity.this, MultiImageSelectorActivity.class); intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true); intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 9); intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE); startActivityForResult(intent, 10001);//10001-->添加
2、通過onActivityResult獲取信息:
final ArrayList<String> paths = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT); Bitmap bitmap = BitmapFactory.decodeFile(paths.get(0), DIY_dialog.getBitmapOption(4));
首先設置布局管理器為:
recyclerview.setLayoutManager(new GridLayoutManager(this, 3));
然后設置適配器(這里在代碼里面有詳細的注釋):
public class PassengerAdapter extends RecyclerView.Adapter<PassengerAdapter.ViewHolder> { private Context mContext; private OnItemClickLitener listener;//點擊事件接口 private ArrayList<String> imageUrls; private ImageFetcher imageFetcher; private ViewHolder viewHolder; private View view; /** * 在構造方法中傳入圖片地址的數據 * @param context * @param imageUrls */ public PassengerAdapter(Context context, ArrayList<String> imageUrls) { this.mContext = context; this.imageUrls = imageUrls; //初始化加載網絡圖片的jar包 imageFetcher = new ImageFetcher(context); imageFetcher.setImageCache(ImageCache.getInstance(context)); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { view = LayoutInflater.from(mContext).inflate(R.layout.lay_passager, null); viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { //設置內容為“hehe”的的元素為默認的添加按鈕 if (imageUrls.get(position).equals("hehe")) { holder.imageViewBig.setBackgroundResource(R.mipmap.add); //當圖片是添加按鈕的時候隱藏刪除按鈕 holder.imageViewSmall.setVisibility(View.GONE); } else { holder.imageViewSmall.setVisibility(View.VISIBLE); /** * 判斷圖片路徑是網絡地址還是本地圖片 * 設置路徑之中包含“storage”的為本地圖片 */ if (imageUrls.get(position).contains("storage")) { try { File file = new File(imageUrls.get(position)); //將bitmap轉化成drawable Bitmap bmp = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), Uri.fromFile(file)); Drawable drawable =new BitmapDrawable(bmp); //按比例擴大圖片的size居中顯示,使得圖片長(寬)等于或大于View的長(寬) holder.imageViewBig.setScaleType(ImageView.ScaleType.CENTER_CROP); holder.imageViewBig.setImageBitmap(bmp); } catch (IOException e) { e.printStackTrace(); } // holder.imageViewBig.setImageURI(Uri.parse(imageUrls.get(position))); } else { imageFetcher.loadImage(imageUrls.get(position), holder.imageViewBig, R.mipmap.touxiang); } } } @Override public int getItemCount() { return imageUrls.size(); } public void setOnItemClickLitener(OnItemClickLitener listener) { this.listener = listener; } public interface OnItemClickLitener { void onBigClick(int position); void onSmallClick(int position); } class ViewHolder extends RecyclerView.ViewHolder { ImageView imageViewBig, imageViewSmall; public ViewHolder(View itemView) { super(itemView); imageViewBig = (ImageView) itemView.findViewById(R.id.imageViewBig); imageViewSmall = (ImageView) itemView.findViewById(R.id.imageViewSmall); /** * * 因為元素是變化的,動態的,所以對點擊事件的處理放在ViewHolder類里面,調用getposition() * 可以獲取到當前的元素位子 */ imageViewBig.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = (Integer) v.getTag(); listener.onBigClick(getPosition()); } }); imageViewSmall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = (Integer) v.getTag(); listener.onSmallClick(getPosition()); } }); } } }
recycler的布局文件
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/lay_group" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageViewBig" android:layout_width="120dp" android:layout_height="120dp" android:background="@mipmap/background" /> <ImageView android:id="@+id/imageViewSmall" android:layout_width="20dp" android:layout_height="20dp" android:background="@mipmap/del" android:layout_gravity="right"/> </FrameLayout> </FrameLayout>
配置recyclerview和設置點擊事件
recyclerview = (RecyclerView) findViewById(R.id.recyclerview); recyclerview.setLayoutManager(new GridLayoutManager(this, 3)); if(imageUrls.size()==0){ imageUrls.add("hehe"); } passengerAdapter = new PassengerAdapter(this, imageUrls); recyclerview.setAdapter(passengerAdapter); passengerAdapter.setOnItemClickLitener(new PassengerAdapter.OnItemClickLitener() { @Override public void onBigClick(int position) { Log.d(TAG, "onBigClick: "+passengerAdapter.getItemCount()); Log.d(TAG, "onBigClick: "+position); if (position==imageUrls.size()-1) { //添加本地相冊圖片,更新視圖 Intent intent = new Intent(PassengerSetActivity.this, MultiImageSelectorActivity.class); intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true); intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 9); intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE); startActivityForResult(intent, 10001);//10001-->添加 } else { Intent intent = new Intent(PassengerSetActivity.this, MultiImageSelectorActivity.class); intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true); intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 9); intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE); poss = position; startActivityForResult(intent, 10002);//10002-->修改 //修改圖片,更新視圖 } } @Override public void onSmallClick(int position) { imageUrls.remove(position); passengerAdapter.notifyItemRemoved(position); //刪除圖片,更新視圖 } });
選擇完圖片后,進行ui更新的操作
/** * 返回圖片url,并上傳七牛 * * @param requestCode * @param resultCode * @param data */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(data!=null){ if (requestCode == 10001 || requestCode == 10002) { final ArrayList<String> paths = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT); Bitmap bitmap = BitmapFactory.decodeFile(paths.get(0), DIY_dialog.getBitmapOption(4)); //添加圖片 if (requestCode == 10001) { //將添加的圖片放在第一位 imageUrls.add(0,paths.get(0)); //更新第一個位置的圖片 passengerAdapter.notifyItemInserted(0); } //修改圖片 else if (requestCode == 10002&&imageUrls.size()>0) { int pos = poss; imageUrls.set(pos,paths.get(0)); passengerAdapter.notifyItemChanged(pos); } } } }
大概就是這樣了,簡單的說一下思路,就是在存儲圖片的list的最后一項添加一個具有不同標識的數據,當點擊時對應的標識和該標識一致,則添加,反之為修改。使用recylerview可以直接更新有變化的子項,而不用更新全部子項,用戶體驗更好,性能也提高了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。