My Android Learning Road 01-GridView

Posted by suresh_m76 on Sun, 09 Jun 2019 23:02:35 +0200

This vacation started to learn android. I had known some simple concepts and had done some simple applications before. But they can't do it. This summer vacation, suddenly had a software to introduce Chinese beer to foreigners, decided to give him step by step to achieve, in the process of implementation, learning android, is not beautiful.


My first question is about the view. The app interface in my mind is probably the view of the rotten street, just like that of Jingdong Taobao. I think you should use the GridView view first. So I started learning about GridView.
First of all, we understand the use scenario of grid view. The arrangement of grid view is similar to that of matrix. When many elements (text, picture or other elements) on screen need to be displayed in matrix format (as shown below), we can use the GridView control to achieve it.
![]({{site.baseurl}}/_posts/timg%20(1).jpg)
The official definition of GridView is: A view that shows items in two-dimensional scrolling grid. The items in the grid come from the List Adapter associated with this view.


Its common xml attributes are as follows:

Among them, android:columnWidth[int] is used to set the width of each column; android:gravity[int] is used to set the proportion of each grid; android:horizontalSpacing[int] is used to set the default horizontal distance of columns between grids; android:numColumn[int] is used to set the number of columns; android:stretchMode[int] is used to set the way in which columns should fill the available space; Nt] Sets the default vertical distance of rows between grids.
Following the official tutorial step by step, I first define an Image Adapter, inherit the Base Adapter, declare an integer array in the Image Adapter, store the ID number of the image resources, and then write a getView method to assign the ItemView under the GridView, and the value is the ID and text of the image.


public class ImageAdapter extends BaseAdapter {
    private Context mContext;


    public ImageAdapter(Context c) {
        mContext = c;
    }


    public int getCount() {
        return mThumbIds.length;
    }


    public Object getItem(int position) {
        return null;
    }


    public long getItemId(int position) {
        return 0;
    }


    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }


        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }


    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}




Looking at another article, I found that there is a way to use LayoutInflater to pass the array context. I don't know much about this LayoutInflater, and I intend to learn more about it.

Topics: Android xml