<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了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); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45