当前位置: 移动技术网 > 移动技术>移动开发>Android > Android添加图片到ListView或者RecyclerView显示

Android添加图片到ListView或者RecyclerView显示

2019年07月24日  | 移动技术网移动技术  | 我要评论
先上图   点击+号就去选择图片 实际上这个添加本身就是一个listview或者 recyclerview 只是布局有些特殊 item&nb

先上图

 

点击+号就去选择图片

实际上这个添加本身就是一个listview或者 recyclerview

只是布局有些特殊
item 

<?xml version="1.0" encoding="utf-8"?>
<liu.myrecyleviewchoosephoto.view.squarerelativelayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/rootview"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">


 <relativelayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginleft="7dp"
  android:layout_marginright="7dp"
  android:layout_margintop="14dp"
  android:background="@drawable/shape_white_bg_corner"
  >

  <imageview
   android:id="@+id/ivdisplayitemphoto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:contentdescription="@null"
   android:scaletype="centercrop"
   android:layout_centerinparent="true"
   android:layout_marginright="8dp"
   android:layout_marginleft="8dp"
   android:layout_margintop="5dp"
   android:layout_marginbottom="5dp"
   />

  <imageview
   android:id="@+id/ivaddphoto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_centerinparent="true"
   android:background="@color/white"
   android:scaletype="centercrop"
   android:src="@mipmap/add_photo_refund"
   android:visibility="visible"/>

  <imageview
   android:id="@+id/ivuploadingbg"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:src="@drawable/shape_grey_bg_corner"
   android:visibility="gone"/>

  <imageview
   android:id="@+id/iverror"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerinparent="true"
   android:src="@mipmap/icon_prompt"
   android:visibility="gone"/>

  <textview
   android:id="@+id/tvprogress"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerinparent="true"
   android:text="0%"
   android:textcolor="@color/white"
   android:textsize="16sp"
   android:visibility="gone"/>

 </relativelayout>

 <imageview
  android:id="@+id/ivdelete"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparentright="true"
  android:src="@mipmap/delete_photo"
  android:visibility="gone"/>


</liu.myrecyleviewchoosephoto.view.squarerelativelayout> 

在adpater中判断一个数据是不是为0和是不是最后一个添加的图片就可以了。

 @override
 public int getitemcount() {
  if (mdatas == null || mdatas.size() == 0) {
   return 1;
  } else if (mdatas.size() < mmaxnum) {
   return mdatas.size() + 1;
  } else {
   return mdatas.size();
  }
 }

这里用到了一个正方形的,容器 

package liu.myrecyleviewchoosephoto.view;


import android.content.context;
import android.util.attributeset;
import android.widget.relativelayout;

/**
 * 正方形的relativelayout
 * created by 刘楠 on 2016/8/13 0013.16:07
 */
public class squarerelativelayout extends relativelayout {
 public squarerelativelayout(context context) {
  super(context);
 }

 public squarerelativelayout(context context, attributeset attrs) {
  super(context, attrs);
 }

 public squarerelativelayout(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {


  //设置自己测量结果
  setmeasureddimension(getdefaultsize(0,widthmeasurespec),getdefaultsize(0,heightmeasurespec));


  /**
   * 测量子view的
   */
  int childwidthsize=getmeasuredwidth();
  //高度与宽度一样
  widthmeasurespec =measurespec.makemeasurespec(childwidthsize,measurespec.exactly);
  heightmeasurespec =widthmeasurespec;

  super.onmeasure(widthmeasurespec, heightmeasurespec);
 }
} 

这里没有写图片选择器
有兴趣可以看这里
图片选择器:https://github.com/ln0491/photoview 
源码:https://github.com/ln0491/myrecyleviewchoosephoto

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网