2014-02-05 31 views
1

我正在使用网格视图来显示我的数据[即一个图像和它的摇摆]get GridView项目精确的正方形

而我的图像是动态加载与我的图像加载器库。问题是我的一些图像是横向或纵向。由于这个我的网格元素不保持精确的平方比。有些则是垂直拉伸或压缩。

<GridView 
    android:id="@+id/fcpl_grid_listview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:numColumns="2" 
    android:scrollingCache="false"/> 

这里是我的getView方法:

public View getView(final int position, View convertView, final ViewGroup parent) { 
      final ViewHolder viewholder; 
      Log.e("Calling...","getView"); 
      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.inflater_product_list, parent, false); 
       viewholder = new ViewHolder(); 
       viewholder.productImage = (ImageView) convertView.findViewById(R.id.ipl_imageview); 
       viewholder.backGroundLayout = (RelativeLayout) convertView.findViewById(R.id.ipl_bk_layout); 
       viewholder.productName = (TextView) convertView.findViewById(R.id.ipl_productname); 
       convertView.setTag(viewholder); 
      } else { 
       viewholder = (ViewHolder) convertView.getTag(); 
      } 
      viewholder.productName.setText("SOME TEXT"); 
      String imageUrl = "SOME URL"; 
      imageLoader.get(imageUrl, VALUES_FOR_MY_IMAGE_LOADER_LIBRARY)); 
      return convertView; 
     } 

我试着在网上不同的建议的方法,但没有工作,因为我有动态图像加载当getView叫做ImageView的的LayoutParams不知道。

+0

为什么不修正'ImageView'的高度和宽度? – GrIsHu

+0

不,我不能;我必须保持GridView宽度作为填充父项;网格视图中有2列。所以我不知道确切的高度[即高度=宽度] – AabidMulani

+0

你必须刚刚固定图像的高度。 –

回答

1

我建议你做类延伸ImageView(或GridView)并测量根或任何其他父视图的范围。然后在你的项目xml中使用它。

public class SquareImageView extends ImageView { 
    public SquareImageView(Context context) { 
     super(context); 
    } 

    public SquareImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
     int height = MeasureSpec.getSize(heightMeasureSpec); 
     View rootView = this.getRootView(); 
     if (rootView != null) { 
      width = rootView.getWidth(); 
      height = rootView.getHeight(); 
     } 
     int size = width > height ? height : width; 
     super.onMeasure(size, size); 
     setMeasuredDimension(size, size); 
    } 
}