欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-31 15:22:10
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下 mainactivity.java代码: package siso.nin...

本文实例为大家分享了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: chjie.jaeger@gamil.com
 * 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: chjie.jaeger@gamil.com
 * 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",
 };

 @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 + 1));
 post post = new post("看图,字不重要。想看大图?抱歉我还没做这个 ( •̀ .̫ •́ )", imgurls);
 mpostlist.add(post);
 }
 mpostadapter = new postadapter(this, mpostlist, 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, parent, 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)

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

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

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