在我的 Android 应用程序中,我在 GridView 中显示照片。在将这些照片加载到 GridView 之前,我对这些照片进行了一些繁重的处理,因此我决定通过覆盖 onConfigurationChanged() 方法自己处理配置更改,该方法仅更改 GridView 的列数,然后将适配器重新分配给它。当我将设备从横向切换到纵向时,一切正常,并且图像占据了整个列空间,正如预期的那样。但是当我将设备从纵向切换到横向时,GridView 中图像的宽度比它需要的要小很多。我正在使用 Glide 库将照片加载到 GridView 中。
我该怎么做才能解决这个问题?
我的代码如下:
public class PhotosAdapter extends BaseAdapter {
private ArrayList<String> images;
public PhotosAdapter(ArrayList<String> images) {
this.images = images;
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object getItem(int position) {
return images.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null) {
//No tenemos vista reciclada, la creamos
row = getLayoutInflater().inflate(R.layout.grid_row, parent, false);
}
TickedImageView thumbV = (TickedImageView) row.findViewById(R.id.thumb);
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int numberOfColumns = Utils.landscape(PhotosActivity.this) ? 5 : 3;
int width = screenWidth / numberOfColumns;
thumbV.setDrawingWidth(width);
//Por si la TickedImageView fue reciclada, la desmarcamos
thumbV.setSelected(selectedPaths.contains(images.get(position)));
thumbV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TickedImageView view = (TickedImageView) v;
if (view.isSelected())
selectedPaths.add(images.get(position));
else
selectedPaths.remove(images.get(position));
}
});
//Cargamos la imagen
Glide.with(PhotosActivity.this)
.load(new File(images.get(position)))
.centerCrop()
.into(thumbV);
Log.d("TAG", images.get(position));
return row;
}
public void removeImage(String image) {
images.remove(image);
}
public String getImage(int position) {
return images.get(position);
}
}