The actual rendering is as above so let’s get started

I’m going to start with an XML file, and then I’m going to add a GridView to that fileApp :layout_constraintHeight_max=”300dp” is set to the maximum height, so that too much content will not fill the screen. Then define the item file, which is the content of the GridView, called select_language_item.xml

Then create a Java file called myGridViewAdapter.java


public class MyGridViewAdapter extends BaseAdapter {
    // Declare a reference
    private Context mContext;   // This Context variable is used when loading third-party images
    private LayoutInflater mlayoutInflater;
    private ArrayList<String> data_list;
    private int mCurrentItem = 0;
    private boolean isClick = false;

    // Create a constructor
    public MyGridViewAdapter(Context context) {
        this.mContext = context;
        // Load the layout file that the control is in into the current class with a LayoutInflate
        mlayoutInflater = LayoutInflater.from(context);
        data_list = new ArrayList<String>();
    }

    public void addList(String note) { // To add elements to the GridView
        if (!data_list.contains(note)) {
            data_list.add(note);
        }
    }

    @Override
    public int getCount() {
        return data_list.size(); // The total number of GridViews is 10
    }

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

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

    // Write a static class to transfer the layout_grid_item control to use
    static class ViewHolder {
        public TextView Grid_textview;
    }

    public void setCurrentIndex(int index) {
        mCurrentItem = index;
    }

    public int getCurrentIndex() {
        return mCurrentItem;
    }

    public void setClick(boolean click) {
        this.isClick = click;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mlayoutInflater.inflate(R.layout.select_language_item, null);
            holder = new ViewHolder();
            holder.Grid_textview = convertView.findViewById(R.id.itemtext);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String firstTxt = data_list.get(0);
        String languageData = data_list.get(position);
        if (mCurrentItem == position && isClick) {// Click the current element to change the background color of the current element
            holder.Grid_textview.setBackground(mContext.getResources().getDrawable(R.drawable.language_type_true));
            holder.Grid_textview.setTextColor(Color.parseColor("#ffffff"));
        } else {// Leave the background colors of the other elements unchanged
            holder.Grid_textview.setBackground(mContext.getResources().getDrawable(R.drawable.language_type_false));
            holder.Grid_textview.setTextColor(Color.parseColor("# 000000"));
        }
        if(languageData.equals(firstTxt) && ! isClick) {// Initializes the selection of an element
            setCurrentIndex(position);
            holder.Grid_textview.setBackground(mContext.getResources().getDrawable(R.drawable.language_type_true));
            holder.Grid_textview.setTextColor(Color.parseColor("#ffffff"));
        }
        String languageTxt = languageData.split("@") [0];
        holder.Grid_textview.setText(languageTxt);
        returnconvertView; }}Copy the code

Then initialize MyGridViewAdapter in the file you want to call

    private var myGridViewAdapter: MyGridViewAdapter? = null
    // Then declare it in the create cycle
    myGridViewAdapter = MyGridViewAdapter(context)
    // Then create a new function to assign
     fun getData(a) {
        var count = 0
        //iconName is a traversable element
        for (i in iconName) {
            count = count + 1myGridViewAdapter? .addList(i +"@"+ count.toString()); }}// called in the create cycle
    getData()
    // Then executegview? .adapter = myGridViewAdapter gview? .setOnItemClickListener(AdapterView.OnItemClickListener { adapterView, view, i, l -> myGridViewAdapter? .setCurrentIndex(i)// Set the current indexmyGridViewAdapter? .setClick(true)// Set whether to clickmyGridViewAdapter? .notifyDataSetChanged()/ / refresh the UI
        })

Copy the code

So a gridView is done