Monodorid上的GridView适配器

时间:2012-10-15 15:47:43

标签: android xamarin.android android-gridview baseadapter xamarin

我一直遇到一个奇怪的问题,我使用自定义适配器从列表填充gridView。

一切看起来很正常,直到gridView的第一页已满,然后一些后续项目丢失或不同步。

我已经完成了两次屏幕截图,第一次是单列,第二次是两次。正如您所看到的,第一页在两者中都正确呈现,如下图所示:

单列:http://screencast.com/t/mSoEsDf1cY

两栏:http://screencast.com/t/i6zfPLYf

我简化了代码来演示问题:

public class Activity1 : Activity
{
    List<Item> _items = null;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        _items = new List<Item>();

        for ( int i =0; i < 30; i++)
        {
            Item newItem = new Item();
            newItem.Text = "Item " + i;
            newItem.Bitmap = Android.Graphics.BitmapFactory.DecodeResource( this.Resources, Resource.Drawable.Icon );

            _items.Add( newItem );
        }

        GridView grid = FindViewById<GridView> (Resource.Id.gridView1);
        grid.SetNumColumns(2);

        ItemGridViewAdapter adapter = new ItemGridViewAdapter( this, _items);
        grid.Adapter = adapter;
    }

}

public class Item
{
    public Android.Graphics.Bitmap Bitmap { get; set; }
    public string Text { get; set; }
}

public class ItemGridViewAdapter : BaseAdapter<Item>
{
    private List<Item> _items = null;
    private Context _context = null;

    public override int Count {
        get {
            return _items.Count;
        }
    }

    public override long GetItemId (int position)
    {
        return position;
    }

    public override Item this[int position] {
        get {
            return _items[position];
        }
    }

    public ItemGridViewAdapter (Context context, List<Item> items)
    {
        _items = items;
        _context = context;
    }

    public override View GetView (int position, View convertView, ViewGroup parent)
    {
        //based on http://developer.android.com/guide/topics/ui/layout/gridview.html

        RelativeLayout layout = null;

        if (convertView == null) {
            layout = new RelativeLayout( _context );

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent);

            ImageView image = new ImageView (_context);
            image.LayoutParameters = new Gallery.LayoutParams (150, 150);
            image.SetImageBitmap ( this[position].Bitmap );
            image.Id = 1;
            layout.AddView (image);

            RelativeLayout.LayoutParams title_lp = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent);
            TextView title = new TextView (_context);
            title.Text = this[position].Text; 
            title.Id = 2;
            title.SetTextColor (Android.Graphics.Color.White);
            title_lp.AddRule (LayoutRules.RightOf, 1);
            title_lp.AddRule (LayoutRules.CenterVertical);
            layout.AddView (title, title_lp);

        } else {

            layout = (RelativeLayout)convertView;
        }

        return layout;
    }

}

非常感谢任何想法。

1 个答案:

答案 0 :(得分:1)

您的代码会重复使用一些随机的旧项目视图,而不做任何修改:

    layout = (RelativeLayout)convertView;

您的getView()应该被修改为为任何视图提供新的值,这是位置的函数,无论是回收还是刚创建。

请考虑以下示例:http://developer.android.com/guide/topics/ui/layout/gridview.html

它们与“其他”部分几乎相同。但在那之后,它们为视图提供了新的价值 - 在他们的例子中,是一个新的图像:

} else {
    imageView = (ImageView) convertView;
}

// Analogous update is missing from your code.
// Set your view's image, text, etc. here:
imageView.setImageResource(mThumbIds[position]);

return imageView;