本文实例为大家分享了android实现图片九宫格的具体代码,供大家参考,具体内容如下
九宫格分三类
实现的效果
具体实现
activity_main
<?xml version="1.0" encoding="utf-8"?> <layout 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"> <data> </data> <androidx.constraintlayout.widget.constraintlayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintend_toendof="parent" app:layout_constraintstart_tostartof="parent" app:layout_constrainttop_totopof="parent" /> </androidx.constraintlayout.widget.constraintlayout> </layout>
item_main
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="img" type="com.nooneb.ninegrid.img" /> <import type="android.view.view"/> </data> <androidx.constraintlayout.widget.constraintlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="64dp"> <imageview android:id="@+id/oneimg" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isone()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_toendof="parent" app:layout_constrainthorizontal_bias="0.0" app:layout_constraintstart_tostartof="parent" app:layout_constrainttop_totopof="parent" app:localimg="@{img.img1}" /> <imageview android:id="@+id/twoimg1" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.istwoorfour()?view.visible:view.gone}" app:layout_constraintdimensionratio="w,1:1" app:layout_constraintend_tostartof="@+id/guideline2" app:layout_constraintstart_tostartof="parent" app:layout_constrainttop_totopof="parent" app:localimg="@{img.img1}" /> <imageview android:id="@+id/twoimg2" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.istwoorfour()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_toendof="parent" app:layout_constraintstart_tostartof="@+id/guideline2" app:layout_constrainttop_totopof="parent" app:localimg="@{img.img2}" /> <imageview android:id="@+id/twoimg3" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isfour()?view.visible:view.gone}" app:layout_constraintdimensionratio="w,1:1" app:layout_constraintend_tostartof="@+id/guideline2" app:layout_constraintstart_tostartof="parent" app:layout_constrainttop_tobottomof="@+id/twoimg1" app:localimg="@{img.img3}" /> <imageview android:id="@+id/twoimg4" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isfour()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_toendof="parent" app:layout_constraintstart_tostartof="@+id/guideline2" app:layout_constrainttop_tobottomof="@+id/twoimg2" app:localimg="@{img.img4}" /> <imageview android:id="@+id/threeimg1" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isother()?view.visible:view.gone}" app:layout_constraintdimensionratio="w,1:1" app:layout_constraintend_tostartof="@+id/guideline3" app:layout_constraintstart_tostartof="parent" app:layout_constrainttop_totopof="parent" app:localimg="@{img.img1}" /> <imageview android:id="@+id/threeimg2" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isother()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_tostartof="@+id/guideline4" app:layout_constraintstart_tostartof="@+id/guideline3" app:layout_constrainttop_totopof="parent" app:localimg="@{img.img2}" /> <imageview android:id="@+id/threeimg3" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isother()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_toendof="parent" app:layout_constraintstart_tostartof="@+id/guideline4" app:layout_constrainttop_totopof="parent" app:localimg="@{img.img3}" /> <imageview android:id="@+id/threeimg4" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isother()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_tostartof="@+id/guideline3" app:layout_constraintstart_tostartof="parent" app:layout_constrainttop_tobottomof="@+id/threeimg1" app:localimg="@{img.img4}" /> <imageview android:id="@+id/threeimg5" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isother()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_tostartof="@+id/guideline4" app:layout_constraintstart_tostartof="@+id/guideline3" app:layout_constrainttop_tobottomof="@+id/threeimg2" app:localimg="@{img.img5}" /> <imageview android:id="@+id/threeimg7" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isother()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_tostartof="@+id/guideline3" app:layout_constrainthorizontal_bias="0.0" app:layout_constraintstart_tostartof="parent" app:layout_constrainttop_tobottomof="@+id/threeimg4" app:localimg="@{img.img7}" /> <imageview android:id="@+id/threeimg8" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isother()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_tostartof="@+id/guideline4" app:layout_constrainthorizontal_bias="0.0" app:layout_constraintstart_tostartof="@+id/guideline3" app:layout_constrainttop_tobottomof="@+id/threeimg5" app:localimg="@{img.img8}" /> <imageview android:id="@+id/threeimg6" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isother()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_toendof="parent" app:layout_constraintstart_tostartof="@+id/guideline4" app:layout_constrainttop_tobottomof="@+id/threeimg3" app:localimg="@{img.img6}" /> <imageview android:id="@+id/threeimg9" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@{img.isother()?view.visible:view.gone}" app:layout_constraintdimensionratio="1:1" app:layout_constraintend_toendof="parent" app:layout_constrainthorizontal_bias="0.0" app:layout_constraintstart_tostartof="@+id/guideline4" app:layout_constrainttop_tobottomof="@+id/threeimg6" app:localimg="@{img.img9}" /> <androidx.constraintlayout.widget.guideline android:id="@+id/guideline2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintguide_percent=".5" /> <androidx.constraintlayout.widget.guideline android:id="@+id/guideline3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintguide_percent=".333333" /> <androidx.constraintlayout.widget.guideline android:id="@+id/guideline4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintguide_percent=".666666" /> </androidx.constraintlayout.widget.constraintlayout> </layout>
实体类
public class img { public integer img1; public integer img2; public integer img3; public integer img4; public integer img5; public integer img6; public integer img7; public integer img8; public integer img9; public img(integer img1, integer img2, integer img3, integer img4, integer img5, integer img6, integer img7, integer img8, integer img9) { this.img1 = img1; this.img2 = img2; this.img3 = img3; this.img4 = img4; this.img5 = img5; this.img6 = img6; this.img7 = img7; this.img8 = img8; this.img9 = img9; } public int count(){ int i=0; if (img1!=null)i++; if (img2!=null)i++; if (img3!=null)i++; if (img4!=null)i++; if (img5!=null)i++; if (img6!=null)i++; if (img7!=null)i++; if (img8!=null)i++; if (img9!=null)i++; return i; } public boolean isone(){ return count()==1; } public boolean istwoorfour(){ return count()==2||count()==4; } public boolean isfour(){ return count()==4; } public boolean isother(){ if (count()!=1){ if (count()!=2){ return count() != 4; } } return false; } }
图片适配器
public class imgadapter { @bindingadapter("localimg") public static void set(imageview imageview,integer res){ if (res==null){ imageview.setvisibility(view.gone); return; } imageview.setimageresource(res); } }
列表适配器
public class myadapter extends recyclerview.adapter<myadapter.holder> { private final context context; public list<img> imgs; public myadapter(context context,list<img> imgs) { this.context = context; this.imgs=imgs; } @nonnull @override public holder oncreateviewholder(@nonnull viewgroup parent, int viewtype) { itemimgbinding binding = itemimgbinding.inflate( layoutinflater.from(context), parent, false); return new holder(binding); } @override public void onbindviewholder(@nonnull holder holder, int position) { img img = imgs.get(position); holder.binding.setimg(img); holder.binding.executependingbindings(); } @override public int getitemviewtype(int position) { return position; } @override public int getitemcount() { return imgs.size(); } public class holder extends recyclerview.viewholder { itemimgbinding binding; public holder(itemimgbinding binding) { super(binding.getroot()); this.binding=binding; } } }
mainactivity
public class mainactivity extends appcompatactivity { activitymainbinding binding; myadapter myadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); binding = activitymainbinding.inflate(getlayoutinflater()); setcontentview(binding.getroot()); list<img> imgs = arrays.aslist( new img(r.drawable.avatar_1, null, null, null, null, null, null, null, null), new img(r.drawable.avatar_1, r.drawable.avatar_2, null, null, null, null, null, null, null), new img(r.drawable.avatar_1, r.drawable.avatar_2, r.drawable.avatar_3, null, null, null, null, null, null), new img(r.drawable.avatar_1, r.drawable.avatar_2, r.drawable.avatar_3, r.drawable.avatar_4, null, null, null, null, null), new img(r.drawable.avatar_1, r.drawable.avatar_2, r.drawable.avatar_3, r.drawable.avatar_4, r.drawable.avatar_5, null, null, null, null), new img(r.drawable.avatar_1, r.drawable.avatar_2, r.drawable.avatar_3, r.drawable.avatar_4, r.drawable.avatar_5, r.drawable.avatar_6, null, null, null), new img(r.drawable.avatar_1, r.drawable.avatar_2, r.drawable.avatar_3, r.drawable.avatar_4, r.drawable.avatar_5, r.drawable.avatar_6, r.drawable.avatar_7, null, null), new img(r.drawable.avatar_1, r.drawable.avatar_2, r.drawable.avatar_3, r.drawable.avatar_4, r.drawable.avatar_5, r.drawable.avatar_6, r.drawable.avatar_7, r.drawable.avatar_8, null), new img(r.drawable.avatar_1, r.drawable.avatar_2, r.drawable.avatar_3, r.drawable.avatar_4, r.drawable.avatar_5, r.drawable.avatar_6, r.drawable.avatar_7, r.drawable.avatar_8, r.drawable.avatar_9) ); myadapter=new myadapter(this,imgs); binding.recyclerview.setlayoutmanager(new linearlayoutmanager(this)); binding.recyclerview.additemdecoration(new divideritemdecoration(this,divideritemdecoration.vertical)); binding.recyclerview.setadapter(myadapter); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。