在 GridViewAdapter 类中尝试以下代码
public class GridViewAdapter extends BaseAdapter {
private Context context;
private ArrayList<Integer> imageArrayList = new ArrayList<>();
private int type;
public GridViewAdapter(Context context, ArrayList<Integer> imageArrayList) {
this.context = context;
this.imageArrayList = imageArrayList;
}
@Override
public int getCount() {
return imageArrayList.size();
}
@Override
public Object getItem(int position) {
return imageArrayList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if(convertView==null){
LayoutInflater inflater = LayoutInflater.from(context);
type = getItemViewType(position);
if(type==0) {
convertView = inflater.inflate(R.layout.big_layout,parent,false);
}else {
convertView = inflater.inflate(R.layout.small_layout, parent, false);
}
viewHolder.imageView = convertView.findViewById(R.id.imageView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(imageArrayList.get(position)));
return convertView;
}
@Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
@Override
public int getItemViewType(int position) {
if(position==0) {
return 0;
}else{
return 1;
}
}
private class ViewHolder{
ImageView imageView;
}
}