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

先上图

6e9d12fea497ef51acf3200192655996.gif

点击+号就去选择图片

实际上这个添加本身就是一个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);

}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值