本文介紹了Android用RecyclerView實(shí)現(xiàn)動態(tài)添加本地圖片,分享給大家,具體如下:
創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)雁江,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
本文所用的多圖選擇的library來自:https://github.com/lovetuzitong/MultiImageSelector
簡單介紹一下用法:
1、跳轉(zhuǎn)到圖片選擇頁面:
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));
首先設(shè)置布局管理器為:
recyclerview.setLayoutManager(new GridLayoutManager(this, 3));
然后設(shè)置適配器(這里在代碼里面有詳細(xì)的注釋):
public class PassengerAdapter extends RecyclerView.Adapter<PassengerAdapter.ViewHolder> { private Context mContext; private OnItemClickLitener listener;//點(diǎn)擊事件接口 private ArrayList<String> imageUrls; private ImageFetcher imageFetcher; private ViewHolder viewHolder; private View view; /** * 在構(gòu)造方法中傳入圖片地址的數(shù)據(jù) * @param context * @param imageUrls */ public PassengerAdapter(Context context, ArrayList<String> imageUrls) { this.mContext = context; this.imageUrls = imageUrls; //初始化加載網(wǎng)絡(luò)圖片的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) { //設(shè)置內(nèi)容為“hehe”的的元素為默認(rèn)的添加按鈕 if (imageUrls.get(position).equals("hehe")) { holder.imageViewBig.setBackgroundResource(R.mipmap.add); //當(dāng)圖片是添加按鈕的時候隱藏刪除按鈕 holder.imageViewSmall.setVisibility(View.GONE); } else { holder.imageViewSmall.setVisibility(View.VISIBLE); /** * 判斷圖片路徑是網(wǎng)絡(luò)地址還是本地圖片 * 設(shè)置路徑之中包含“storage”的為本地圖片 */ if (imageUrls.get(position).contains("storage")) { try { File file = new File(imageUrls.get(position)); //將bitmap轉(zhuǎn)化成drawable Bitmap bmp = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), Uri.fromFile(file)); Drawable drawable =new BitmapDrawable(bmp); //按比例擴(kuò)大圖片的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); /** * * 因?yàn)樵厥亲兓模瑒討B(tài)的,所以對點(diǎn)擊事件的處理放在ViewHolder類里面,調(diào)用getposition() * 可以獲取到當(dāng)前的元素位子 */ 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和設(shè)置點(diǎn)擊事件
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); //刪除圖片,更新視圖 } });
選擇完圖片后,進(jìn)行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的最后一項(xiàng)添加一個具有不同標(biāo)識的數(shù)據(jù),當(dāng)點(diǎn)擊時對應(yīng)的標(biāo)識和該標(biāo)識一致,則添加,反之為修改。使用recylerview可以直接更新有變化的子項(xiàng),而不用更新全部子項(xiàng),用戶體驗(yàn)更好,性能也提高了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站標(biāo)題:Android用RecyclerView實(shí)現(xiàn)動態(tài)添加本地圖片
URL分享:http://jinyejixie.com/article26/peosjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、軟件開發(fā)、標(biāo)簽優(yōu)化、網(wǎng)站改版、電子商務(wù)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)