欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android用RecyclerView实现动态添加本地图片

程序员文章站 2023-11-16 20:24:04
本文介绍了android用recyclerview实现动态添加本地图片,分享给大家,具体如下: 本文所用的多图选择的library来自:https://github.co...

本文介绍了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可以直接更新有变化的子项,而不用更新全部子项,用户体验更好,性能也提高了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。