I have a Gridview
in Android where each element is inflated from xml and contains a LinearLayout
holding an ImageView
and a TextView
. The GridView
uses a custom Adapter which extends BaseAdapter
. What I see is that each row rather than being just high enough to hold the largest element, is as tall as the screen.
Here is the relevant code:
The gridview's xml:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ConSoul"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="160dp"
android:horizontalSpacing="20dp"
android:verticalSpacing="0dp"
android:gravity="center"
android:stretchMode="columnWidth" />
The element's xml (thumbnail.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/screen"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scaleType="fitEnd"/>
<TextView
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="20dp"
android:textColor="@color/theme"
android:gravity="center_horizontal"/>
</LinearLayout>
The adapter getView
method (mContext
is an instance variable the adapter is created from, screens is a Bitmap[]
and names is a String[]
):
public View getView(int position, View convertView, ViewGroup parent) {
View thumbView;
if (convertView == null) { // if it's not recycled, initialize some attributes
thumbView = LayoutInflater.from(mContext).inflate(R.layout.thumbnail, null);
} else {
thumbView = convertView;
}
((ImageView)thumbView.findViewById(R.id.screen)).setImageBitmap(screens[position]);
((TextView)thumbView.findViewById(R.id.name)).setText(names[position]);
return thumbView;
}