PatrickXu PatrickXu - 5 months ago 28
Android Question

ImageView's size can't adapt with image from net in GridView

Image File

I have a question , it make me tired whole day.
Why ImageView's background is show when load net picture into ImageView in GridView? ImageView's size can't wrap_content.

How could I fix it? Make ImageView's size adapt picture size (don't change the piacture size)
thanks advance.

A .Main XML


<ListView
android:id="@+id/gridview_listview"
android:layout_width="100dp"
android:background="#00000000"
android:scrollbars="none"
android:dividerHeight="5dp"
android:divider="@color/colorAlpha"
android:listSelector="@drawable/list_item_selected"
android:layout_height="match_parent" />

<GridView
android:id="@+id/gridview"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_width="0dp"
android:scrollbars="none"
android:listSelector="@drawable/gridview_item_selected"
android:layout_height="wrap_content">
</GridView>




B.GridView Item XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="1dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/iv_gridview"
android:padding="0dp"
android:background="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tv_gridview"
android:text="test"
android:textColor="#00ffff"
android:textSize="15sp"
android:gravity="center"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />




C.GridView adapter extends BaseAdapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder gridViewHolder = new GridViewHolder();
ImageView imageView;
TextView textView;
if (convertView == null ) {
convertView = layoutInflater.inflate(R.layout.gridview_item , null) ;
imageView = (ImageView) convertView.findViewById(R.id.iv_gridview);
textView = (TextView) convertView.findViewById(R.id.tv_gridview);
gridViewHolder.setImageView(imageView);
gridViewHolder.setTextView(textView);
convertView.setTag(gridViewHolder);
}else {
gridViewHolder = (GridViewHolder) convertView.getTag();
}
Picasso.with(context).load("http://img1.cache.netease.com/catchpic/A/A8/A86B03AE11945B98D0AE0A98480D28CE.jpg").into(gridViewHolder.getImageView());
gridViewHolder.getTextView().setText(dataList.get(position).getName());
return convertView;
}


D.Activity code

dataList = GetData.getData();
GridViewAdapter gridViewAdapter = new GridViewAdapter(GridView.this , dataList);
gridView.setAdapter(gridViewAdapter);
gridView.setNumColumns(3);
gridView.setHorizontalSpacing(5);
gridView.setVerticalSpacing(10);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url = "this is " + position ;
startActivity(new Intent(GridView.this , ClickActivity.class).putExtra("url" ,url));

}
});

Answer

Try setting the android:adjustViewBounds attribute on your ImageView to true in your layout XML.

<ImageView
    android:id="@+id/iv_gridview"
    android:padding="0dp"
    android:background="#ff0000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true" />
Comments