Android学习教程之九宫格图片展示(13)

Android学习教程之九宫格图片展示(13),第1张

概述本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

本文实例为大家分享了AndroID九宫格图片展示的具体代码,供大家参考,具体内容如下

MainActivity.java代码:

package siso.ninegrIDimg;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;public class MainActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); }}

GrIDStyleActivity.java代码:

package siso.ninegrIDimg;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.support.v7.Widget.Toolbar;import com.jaeger.ninegrIDimagevIEw.NineGrIDImageVIEw;import com.jaeger.ninegrIDimgdemo.R;import com.jaeger.ninegrIDimgdemo.adapter.PostAdapter;import com.jaeger.ninegrIDimgdemo.entity.Post;import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * Created by Jaeger on 16/2/24. * * Email: [email protected] * GitHub: https://github.com/laobIE */public class GrIDStyleActivity extends AppCompatActivity { private RecyclerVIEw mRvPostLister; private PostAdapter mNineImageAdapter; private List<Post> mPostList; private String[] img_URL_List = { "http://ac-QYgvX1CC.clouddn.com/36f0523ee1888a57.jpg","http://ac-QYgvX1CC.clouddn.com/07915a0154ac4a64.jpg","http://ac-QYgvX1CC.clouddn.com/9ec4bc44bfaf07ed.jpg","http://ac-QYgvX1CC.clouddn.com/fa85037f97e8191f.jpg","http://ac-QYgvX1CC.clouddn.com/de13315600ba1cff.jpg","http://ac-QYgvX1CC.clouddn.com/15c5c50e941ba6b0.jpg","http://ac-QYgvX1CC.clouddn.com/10762c593798466a.jpg","http://ac-QYgvX1CC.clouddn.com/eaf1c9d55c5f9afd.jpg","http://ac-QYgvX1CC.clouddn.com/ad99de83e1e3f7d4.jpg","http://ac-QYgvX1CC.clouddn.com/233a5f70512befcc.jpg",}; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_recycler); setSupportActionbar((Toolbar) findVIEwByID(R.ID.toolbar)); mRvPostLister = (RecyclerVIEw) findVIEwByID(R.ID.rv_post_List); mRvPostLister.setLayoutManager(new linearlayoutmanager(this)); mPostList = new ArrayList<>(); for (int i = 0; i < 18; i++) { List<String> imgurls = new ArrayList<>(); imgurls.addAll(Arrays.asList(img_URL_List).subList(0,i % 9)); Post post = new Post("Am I handsome? Am I handsome? Am I handsome?",imgurls); mPostList.add(post); } mNineImageAdapter = new PostAdapter(this,mPostList,NineGrIDImageVIEw.STYLE_GRID); mRvPostLister.setAdapter(mNineImageAdapter); }}

FillStyleActivity.java代码:

package siso.ninegrIDimg;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.support.v7.Widget.Toolbar;import com.jaeger.ninegrIDimagevIEw.NineGrIDImageVIEw;import com.jaeger.ninegrIDimgdemo.R;import com.jaeger.ninegrIDimgdemo.adapter.PostAdapter;import com.jaeger.ninegrIDimgdemo.entity.Post;import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * Created by Jaeger on 16/2/24. * * Email: [email protected] * GitHub: https://github.com/laobIE */public class FillStyleActivity extends AppCompatActivity { private RecyclerVIEw mRvPostLister; private PostAdapter mPostAdapter; private List<Post> mPostList; private String[] img_URL_List = { "https://pic4.zhimg.com/02685b7a5f2d8cbf74e1fd1ae61d563b_xll.jpg","https://pic4.zhimg.com/fc04224598878080115ba387846eabc3_xll.jpg","https://pic3.zhimg.com/d1750bd47b514ad62af9497bbe5bb17e_xll.jpg","https://pic4.zhimg.com/da52c865cb6a472c3624a78490d9a3b7_xll.jpg","https://pic3.zhimg.com/0c149770fc2e16f4a89e6fc479272946_xll.jpg","https://pic1.zhimg.com/76903410e4831571e19a10f39717988c_xll.png","https://pic3.zhimg.com/33c6cf59163b3f17ca0c091a5c0d9272_xll.jpg","https://pic4.zhimg.com/52e093cbf96fd0d027136baf9b5cdcb3_xll.png","https://pic3.zhimg.com/f6dc1c1cecd7ba8f4c61c7c31847773e_xll.jpg",i % 9 + 1)); Post post = new Post("看图,字不重要。想看大图?抱歉我还没做这个 ( •̀ .̫ •́ )",imgurls); mPostList.add(post); } mPostAdapter = new PostAdapter(this,NineGrIDImageVIEw.STYLE_FILL); mRvPostLister.setAdapter(mPostAdapter); }}

PostAdapter.java代码:

package siso.ninegrIDimg.adapter;import androID.content.Context;import androID.support.v7.Widget.RecyclerVIEw;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;import java.util.List;import siso.ninegrIDimg.R;import siso.ninegrIDimg.entity.Post;import siso.nineimglib.NineGrIDImageVIEw;import siso.nineimglib.NineGrIDImageVIEwAdapter;public class PostAdapter extends RecyclerVIEw.Adapter<PostAdapter.PostVIEwHolder> { private LayoutInflater mInflater; private List<Post> mPostList; private int mShowStyle; public PostAdapter(Context context,List<Post> postList,int showStyle) { super(); mPostList = postList; mInflater = LayoutInflater.from(context); mShowStyle = showStyle; } @OverrIDe public voID onBindVIEwHolder(PostVIEwHolder holder,int position) { holder.bind(mPostList.get(position)); } @OverrIDe public int getItemCount() { return mPostList.size(); } @OverrIDe public PostVIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) { if (mShowStyle == NineGrIDImageVIEw.STYLE_FILL) { return new PostVIEwHolder(mInflater.inflate(R.layout.item_post_fill_style,parent,false)); } else { return new PostVIEwHolder(mInflater.inflate(R.layout.item_post_grID_style,false)); } } public class PostVIEwHolder extends RecyclerVIEw.VIEwHolder { private NineGrIDImageVIEw mNglContent; private TextVIEw mTvContent; private NineGrIDImageVIEwAdapter<String> mAdapter = new NineGrIDImageVIEwAdapter<String>() { @OverrIDe protected voID ondisplayImage(Context context,ImageVIEw imageVIEw,String s) { Picasso.with(context)  .load(s)  .placeholder(R.drawable.ic_default_image)  .into(imageVIEw); } @OverrIDe protected ImageVIEw generateImageVIEw(Context context) { return super.generateImageVIEw(context); } @OverrIDe protected voID onItemImageClick(Context context,int index,List<String> List) { Toast.makeText(context,"image position is " + index,Toast.LENGTH_SHORT).show(); } }; public PostVIEwHolder(VIEw itemVIEw) { super(itemVIEw); mTvContent = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_content); mNglContent = (NineGrIDImageVIEw) itemVIEw.findVIEwByID(R.ID.ngl_images); mNglContent.setAdapter(mAdapter); } public voID bind(Post post) { mNglContent.setimagesData(post.getimgurlList()); mTvContent.setText(post.getContent()); } }}

Post.java代码:

package siso.ninegrIDimg.entity;import java.util.List;public class Post { private String mContent; private List<String> mimgurlList; public Post() { } public Post(String content,List<String> imgurlList) { mContent = content; mimgurlList = imgurlList; } public String getContent() { return mContent; } public voID setContent(String content) { mContent = content; } public List<String> getimgurlList() { return mimgurlList; } public voID setimgurlList(List<String> imgurlList) { mimgurlList = imgurlList; }}

activity_main.xml:

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="siso.ninegrIDimg.MainActivity"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Hello World!" /></relativeLayout>

item_post_fill_style.xml:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:ngl="http://schemas.androID.com/apk/res-auto"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:layout_margin="8dp"androID:background="@color/white"androID:orIEntation="vertical"androID:padding="8dp"><TextVIEw androID:ID="@+ID/tv_content" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginBottom="8dp" androID:textcolor="#333333" androID:textSize="16sp"/><com.jaeger.ninegrIDimagevIEw.NineGrIDImageVIEw androID:ID="@+ID/ngl_images" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" ngl:imgGap="3dp" ngl:showStyle="fill" ngl:singleimgSize="160dp"/></linearLayout>

item_post_grID_style.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_margin="8dp" androID:background="@color/white" androID:orIEntation="vertical" androID:padding="8dp"> <TextVIEw androID:ID="@+ID/tv_content" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginBottom="8dp" androID:textcolor="#333333" androID:textSize="16sp" tools:text="测试文字"/> <com.jaeger.ninegrIDimagevIEw.NineGrIDImageVIEw androID:ID="@+ID/ngl_images" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" app:imgGap="3dp" app:maxSize="-1" app:showStyle="grID" app:singleimgSize="500dp"/></linearLayout>

item_single_image.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:maxHeight="60dp" androID:maxWIDth="60dp" androID:orIEntation="vertical"> <ImageVIEw androID:ID="@+ID/iv_single" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:scaleType="fitStart"/></linearLayout>

strings.xml

<resources> <string name="app_name">NineGrIDimg</string> <string name="fill_style">Fill Style</string> <string name="grID_style">GrID Style</string></resources>

styles.xml

<resources> <!-- Base application theme. --> <style name="Apptheme" parent="theme.AppCompat.light.DarkActionbar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="androID:textAllCaps">false</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowNoTitle">true</item> <item name="androID:textcolor">@color/white</item> <item name="windowActionbar">false</item> </style></resources>

AndroIDManifest.xml

<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="siso.ninegrIDimg"> <application androID:allowBackup="true" androID:icon="@mipmap/ic_launcher" androID:label="@string/app_name" androID:supportsRtl="true" androID:theme="@style/Apptheme"> <activity androID:name=".MainActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>

build.gradle

apply plugin: 'com.androID.application'androID { compileSdkVersion 23 buildToolsversion "23.0.1" defaultConfig { applicationID "siso.ninegrIDimg" minSdkVersion 22 targetSdkVersion 22 versionCode 1 versionname "1.0" } buildTypes { release { MinifyEnabled false proguardfiles getDefaultProguardfile('proguard-androID.txt'),'proguard-rules.pro' } }}dependencIEs { compile filetree(include: ['*.jar'],dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.androID.support:appcompat-v7:23.0.1' compile 'com.androID.support:recyclervIEw-v7:23.3.0' compile 'com.squareup.picasso:picasso:2.5.2' compile project(path: ':nineimglib')}

AndroID类库项目nineimglib

项目运行结果如图:

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

总结

以上是内存溢出为你收集整理的Android学习教程之九宫格图片展示(13)全部内容,希望文章能够帮你解决Android学习教程之九宫格图片展示(13)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存