我正在尝试在Fragment中的GridView中绑定Image。片段正在加载但它是空白的。我把断点放在ImageAdapeter的GetView方法上,但它没有击中断点。这是片段空白的原因吗?
这是我的代码
片段代码:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ViewGroup root = (ViewGroup)inflater.Inflate(Resource.Layout.profilefragmentlayout, null);
var PhotoGridView = root.FindViewById<GridView>(Resource.Id.PhotoGridView);
PhotoGridView.Adapter = new ImageAdapter(root.Context);
return root;
}
ImageAdapter代码
public class ImageAdapter: BaseAdapter
{
Context contextcreate;
public ImageAdapter(Context a)
{
contextcreate = a;
}
public override int Count
{
get { return imageIds.Length; }
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return 0;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(contextcreate);
imageView.LayoutParameters = new GridView.LayoutParams(50, 50);
imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
imageView.SetPadding(5, 5, 5, 5);
}
else
{
imageView = (ImageView)convertView;
}
imageView.SetImageResource(imageIds[position]);
return imageView;
}
int[] imageIds = {
Resource.Drawable.WP1, Resource.Drawable.WP2,
Resource.Drawable.WP3, Resource.Drawable.WP4,
Resource.Drawable.WP5, Resource.Drawable.WP6,
Resource.Drawable.WP7, Resource.Drawable.WP8,
Resource.Drawable.WP9, Resource.Drawable.WP10,
Resource.Drawable.WP11, Resource.Drawable.WP12,
};
}
}
请告诉我我遗失的内容,即停止在网格视图中将图像绑定在片段中。
答案 0 :(得分:0)
在片段类中添加此代码
public class OneFragment : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.One, null);
var gridview = view.FindViewById<GridView>(Resource.Id.gridview);
gridview.Adapter = new ImageAdapter(Activity);
gridview.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args) {
Toast.MakeText(Activity, args.Position.ToString(), ToastLength.Short).Show();
};
return view;
}
}
添加ImageAdapter类并在ImageAdapter类
中添加此代码 public class ImageAdapter : BaseAdapter
{
Context context;
public ImageAdapter(Context c)
{
context = c;
}
public override int Count
{
get { return thumbIds.Length; }
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public override View GetView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{ // if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.LayoutParameters = new GridView.LayoutParams(85, 85);
imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
imageView.SetPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView)convertView;
}
imageView.SetImageResource(thumbIds[position]);
return imageView;
}
// references to our images
int[] thumbIds = {
Resource.Drawable.Icon, Resource.Drawable.Icon,
Resource.Drawable.Icon, Resource.Drawable.Icon,
Resource.Drawable.Icon, Resource.Drawable.Icon,
Resource.Drawable.Icon, Resource.Drawable.Icon,
Resource.Drawable.Icon, Resource.Drawable.Icon,
Resource.Drawable.Icon, Resource.Drawable.Icon,
Resource.Drawable.Icon, Resource.Drawable.Icon,
Resource.Drawable.Icon, Resource.Drawable.Icon,
Resource.Drawable.Icon, Resource.Drawable.Icon,
};
}
它为我工作