先上图
点击+号就去选择图片
实际上这个添加本身就是一个listview或者 recyclerview
只是布局有些特殊
item
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
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"
>
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"
/>
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"/>
android:id="@+id/ivuploadingbg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/shape_grey_bg_corner"
android:visibility="gone"/>
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"/>
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"/>
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"/>
在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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。