Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize ViewGroup to realize Nine Palace Grid Control in moments by Android

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "Android how to customize ViewGroup to achieve nine palace controls in moments", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to customize ViewGroup to achieve nine grid controls in moments" can help you solve your doubts.

1. Introduction 1.1, the effect picture is as follows

1.2. The main functions are as follows

1: when you take a single image, you can adapt it in the set area according to the aspect ratio of the image.

Bind data and UI in 2:Adapter mode

3: callback of picture click event

4: set the size of the picture interval

5: set the ImageView fillet effect freely through Glide

2. Use

2.1.Custom attributes are as follows. Custom NineImageLayout 2.3and Adapter are used in the layout to bind data and UI.

Glide.asBitmap is used to calculate the width and height of the image. If you have the width and height of the returned image in the background, you can omit this step and directly setSingleImage (width, height,imageView)

Ps: if you can advise the backend to return the width and height of the picture, this can avoid the control's high jump screen when a single picture is taken. For example, when I limit the width and height of a single picture to the 200dp range, to display the width and height of the 1000px and the high 500px, the width and height of the control will be 200dp before the picture is loaded, and the height will be changed to 100dp after the picture is loaded, which will have a bad user experience. Therefore, it is recommended to record the width and height of the picture when uploading the picture.

NineImageLayout.setAdapter (new NineImageAdapter () {@ Override protected int getItemCount () {return mData.size ();} @ Override protected View createView (LayoutInflater inflater, ViewGroup parent, int I) {return inflater.inflate (R.layout.item_img_layout, parent, false) } @ Override protected void bindView (View view, final int i) {final ImageView imageView = view.findViewById (R.id.iv_img); Glide.with (mContext) .load (mData.get (I)) .into (imageView) If (mData.size () = = 1) {Glide.with (mContext) .asBitmap () .load (mData.get (0)) .into (new SimpleTarget () {@ Override) Public void onResourceReady (Bitmap bitmap Transition

Construct the test data in Activity. The approximate code is as follows

Public class NineImageLayoutActivity extends AppCompatActivity {private RecyclerView mRecyclerView; private MyAdapter mAdapter; private Random random; private final String URL_IMG = "http://q3x62hkt1.bkt.clouddn.com/banner/58f57dfa5bb73.jpg"; private final String URL_IMG_2 =" http://q3x62hkt1.bkt.clouddn.com/timg.jpeg"; private List mList = new ArrayList (); @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activity_nine_image_layout); random = new Random (); List testList = new ArrayList (); testList.add (URL_IMG_2); for (int I = 0; I < 100; iTunes +) {int count = I% 9 + 1; List list = new ArrayList (); for (int j = 0; j < count) If (I% 8 = = 0) {mList.add (testList);} mList.add (list);} mRecyclerView = findViewById (R.id.recyclerview); mAdapter = new MyAdapter (mList, this); mRecyclerView.setLayoutManager (new LinearLayoutManager (this)) MRecyclerView.setAdapter (mAdapter);}}

Setting data in MyAdapter

Import java.util.List;/** * @ author: ChenYangQi * date: 13:49 on 2020-1-16 * desc: * / public class MyAdapter extends RecyclerView.Adapter {private List mList; private Context mContext; public MyAdapter (List mList, Context mContext) {this.mList = mList; this.mContext = mContext } @ NonNull @ Override public MyViewHolder onCreateViewHolder (@ NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from (mContext) .propagate (R.layout.item_nine_img_layout_list, parent, false); return new MyViewHolder (view);} @ Override public void onBindViewHolder (@ NonNull final MyViewHolder holder, final int position) {final List mData = mList.get (position) Holder.tvTitle.setText ("this is" + mData.size () + "title of picture"); final NineImageLayout nineImageLayout = holder.nineImageLayout; holder.nineImageLayout.setAdapter (new NineImageAdapter () {@ Override protected int getItemCount () {return mData.size ()) } @ Override protected View createView (LayoutInflater inflater, ViewGroup parent, int I) {return inflater.inflate (R.layout.item_img_layout, parent, false);} @ Override protected void bindView (View view, final int i) {final ImageView imageView = view.findViewById (R.id.iv_img) Glide.with (mContext) .load (mData.get (I)) .into (imageView) If (mData.size () = = 1) {Glide.with (mContext) .asBitmap () .load (mData.get (0)) .into (new SimpleTarget () {@ Override) Public void onResourceReady (Bitmap bitmap Transition

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report