几年前的一个bug,GridView的item高度不一致。如下图:
复现bug的代码:
import android.os.Bundle;
import android.widget.BaseAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private GridView grid_photo;private BaseAdapter mAdapter = null;private ArrayList<String> mData = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);grid_photo = (GridView) findViewById(R.id.grid_photo);mData = new ArrayList<String>();mData.add( "鸡腿汉堡 x 1");mData.add( "韩国牛排(7分熟、不放辣)x 3");mData.add( "小杯可乐");mData.add( "薯条");mData.add( "冰淇淋");mData.add( "红茶");mData.add( "披萨");mAdapter = new MyAdapter<String>(mData, R.layout.item_grid_icon) {@Overridepublic void bindView(ViewHolder holder, String obj) {holder.setText(R.id.txt_icon, obj);}};grid_photo.setAdapter(mAdapter);}
}//activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="5dp"tools:context=".MainActivity"><!--numColumns设置每行显示多少个--><GridViewandroid:id="@+id/grid_photo"android:layout_width="match_parent"android:layout_height="match_parent"android:horizontalSpacing="2dp"android:verticalSpacing="2dp"android:gravity="center"android:numColumns="2" /></RelativeLayout>//MyAdapter.java
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;public abstract class MyAdapter<T> extends BaseAdapter {private ArrayList<T> mData;private int mLayoutRes; //布局idpublic MyAdapter() {}public MyAdapter(ArrayList<T> mData, int mLayoutRes) {this.mData = mData;this.mLayoutRes = mLayoutRes;}@Overridepublic int getCount() {return mData != null ? mData.size() : 0;}@Overridepublic T getItem(int position) {return mData.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder = ViewHolder.bind(parent.getContext(), convertView, parent, mLayoutRes, position);bindView(holder, getItem(position));return holder.getItemView();}public abstract void bindView(ViewHolder holder, T obj);public static class ViewHolder {private SparseArray<View> mViews; //存储ListView 的 item中的Viewprivate View item; //存放convertViewprivate int position; //游标private Context context; //Context上下文//构造方法,完成相关初始化private ViewHolder(Context context, ViewGroup parent, int layoutRes) {mViews = new SparseArray<>();this.context = context;View convertView = LayoutInflater.from(context).inflate(layoutRes, parent, false);convertView.setTag(this);item = convertView;}//绑定ViewHolder与itempublic static ViewHolder bind(Context context, View convertView, ViewGroup parent,int layoutRes, int position) {ViewHolder holder;if (convertView == null) {holder = new ViewHolder(context, parent, layoutRes);} else {holder = (ViewHolder) convertView.getTag();holder.item = convertView;}holder.position = position;return holder;}@SuppressWarnings("unchecked")public <T extends View> T getView(int id) {T t = (T) mViews.get(id);if (t == null) {t = (T) item.findViewById(id);mViews.put(id, t);}return t;}/*** 设置文字*/public ViewHolder setText(int id, CharSequence text) {View view = getView(id);if (view instanceof TextView) {((TextView) view).setText(text);}return this;}}}//item_grid_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#00ff00"android:padding="5dp"><TextViewandroid:id="@+id/txt_icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_centerHorizontal="true"android:layout_marginTop="30dp"android:text="呵呵"android:textSize="18sp" /></RelativeLayout>
下一篇:android Gridview item高度适应行最大高度问题,讲解决方案。