在我的Android應用程序中,我在GridView中顯示照片。在將這些照片加載到GridView之前,我會對這些照片進行大量處理,因此我決定通過重寫onConfigurationChanged()方法來處理配置更改,該方法僅更改GridView的列數,然後將適配器重新分配給它。 當我將設備從橫向切換到縱向時,所有工作都完美無缺,圖像佔用了整個列空間,如預期的那樣。 但是,當我將設備從縱向切換到橫向時,GridView中圖像的寬度遠遠小於它需要的寬度。 我正在使用Glide庫將照片加載到GridView中。Android - 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);
}
}