What is the method used for Android GridView?

The method for using GridView in Android involves the following steps:

  1. Add a GridView control in the layout file.
<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    android:columnWidth="100dp"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    />

Among them, the numColumns property is used to set the number of columns, the columnWidth property is used to set the width of each column, the verticalSpacing property is used to set the spacing between rows, the horizontalSpacing property is used to set the spacing between columns, and the stretchMode property is used to set whether the width of the items is evenly distributed.

  1. In the Activity, access the GridView control and set the adapter.
GridView gridView = findViewById(R.id.gridview);
gridView.setAdapter(adapter);

The adapter is the Gridview’s adapter, which is used to set the content and style of the items.

  1. Create an adapter class that extends BaseAdapter and implements the necessary methods.
public class MyAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mData;

    public MyAdapter(Context mContext, List<String> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item, null);
            viewHolder = new ViewHolder();
            viewHolder.textView = convertView.findViewById(R.id.textview);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textView.setText(mData.get(position));
        return convertView;
    }

    private static class ViewHolder{
        TextView textView;
    }
}

In the getView() method, set the content of the child item to the data at the corresponding position in mData.

  1. Create the subitem’s layout file grid_item.xml.
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"/>
</LinearLayout>

Customize the layout and style of sub-items according to your needs.

  1. Finally, set up the click event for the GridView in the Activity.
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 处理点击事件
    }
});

The click event of GridView items can be handled in the onItemClick() method.

The above is the basic method of using GridView, where custom layouts can be achieved by setting the content and style of each item through an adapter.

bannerAds