Kevin F
Kevin F

Reputation: 169

Xamarin: Create a custom grid View

I need create a custom GridView in Android Application (Dev. on VS2012 with Xamarin plugin). I need on each cell a image and label text.

This example work perfectly, but has not a text label. http://developer.xamarin.com/guides/android/user_interface/grid_view/

I need anything how this: Custom Grid View {Image+Text}

but I only found Java Examples. Please Help me

PD: Sorry by Image, but I can't upload images (I need more reputation).

Upvotes: 2

Views: 8705

Answers (1)

Tom Opgenorth
Tom Opgenorth

Reputation: 1511

Create a new layout that has the ImageView & TextView. Then, in the GetView method of the ImageAdapter class, inflate that layout and populate it, for example:

public override View GetView (int position, View convertView, ViewGroup parent)
{
    View view;

    if (convertView == null) 
    {  
        view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.my_grid_row, parent, false);
    }
    else
    {
        view = convertView;
    }


    var imageView = view.FindViewById<ImageView>(Resource.Id.my_image_view);
    imageView.SetImageResource (thumbIds[position]);

    var textView = view.FindViewById<TextView>(Resource.Id.my_text_view);
    textView.Text = "Text to Display";

    return view;
}

Edit: (Add to Code "(" and ";")

Update: Here is a sample layout file, my_grid_row.axml, that is inflated by the GetView method:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:scaleType="centerCrop"
        android:id="@+id/my_image_vieww" />
    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/my_text_view" />
</LinearLayout>

Upvotes: 3

Related Questions