gpt4 book ai didi

android - Monodorid 上的 GridView 适配器

转载 作者:行者123 更新时间:2023-11-29 02:00:33 24 4
gpt4 key购买 nike

我在使用自定义适配器从列表中填充 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;
}

}

任何想法将不胜感激。

最佳答案

您的代码在这里没有任何修改地重用了一些随机的旧项目 View :

    layout = (RelativeLayout)convertView;

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

考虑这里的例子:http://developer.android.com/guide/topics/ui/layout/gridview.html .

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

} 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;

关于android - Monodorid 上的 GridView 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12899257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com