Add images to ListView or RecyclerView for Android display,

Source: Internet
Author: User

Add images to ListView or RecyclerView for Android display,

 

First

 

Click the plus sign to select an image.

In fact, this addition itself is a ListView or RecyclerView.

 

The layout is special.

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>

In Adpater, you can determine whether a data value is 0 and whether it is the last added image.

 @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();        }    }

 

Here we use a square container.

Package liu. myrecyleviewchoosephoto. view; import android. content. context; import android. util. attributeSet; import android. widget. relativeLayout;/*** square RelativeLayout * Created by Liu Nan on 2016/8/13 0013.16: 07 */public class 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, expiration) ;}@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {// set your own Measurement Result values (getDefaultSize (0, widthMeasureSpec), getDefaultSize (0, percent )); /*** measure the child View */int childWidthSize = getMeasuredWidth (); // widthMeasureSpec = MeasureSpec with the same height and width. makeMeasureSpec (childWidthSize, MeasureSpec. EXACTLY); heightMeasureSpec = widthMeasureSpec; super. onMeasure (widthMeasureSpec, heightMeasureSpec );}}

 

No image selector is written here.
For more information, see here.

Image selector:

Https://github.com/ln0491/PhotoView

 

Source code:

Https://github.com/ln0491/MyRecyleViewChoosePhoto

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.