本文实例为大家分享了Android实现图片九宫格的具体代码,供大家参考,具体内容如下
九宫格分三类
实现的效果
具体实现
activity_main
item_main
实体类
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{ private final Context context; public List imgs; public MyAdapter(Context context,List 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 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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。