Java 添加GridView的视图
我想为GridView添加页脚视图 我在文档中发现GridView有2个继承的addView(视图子项)方法Java 添加GridView的视图,java,android,inheritance,Java,Android,Inheritance,我想为GridView添加页脚视图 我在文档中发现GridView有2个继承的addView(视图子项)方法 From class android.widgetAdapterView void addView(View child) This method is not supported and throws an UnsupportedOperationException when called. 及 看来我应该用后者。但是我如何才能调用这个特定的继承方法呢?您没有。它会用一个不支持操
From class android.widgetAdapterView
void addView(View child)
This method is not supported and throws an UnsupportedOperationException when called.
及
看来我应该用后者。但是我如何才能调用这个特定的继承方法呢?您没有。它会用一个
不支持操作异常
覆盖原始文件,因为它是。。好。。不支持
您应该改为编辑适配器。根据您的实现,这看起来会有所不同。但是,您只需向适配器添加更多数据,并在适配器上调用.notifyDataSetChanged()
,您的GridView
将自行添加视图
页脚视图应该是一个独立的
视图
,位于网格视图
之后,或者您必须保持其在适配器列表中的位置,以便在您添加新项目时始终是最后一个。提供了一个Erics解决方案的示例,适配器可以保留两个额外的成员来跟踪
“页脚”及其事件处理程序的位置:
class ImageViewGridAdapter : ArrayAdapter<int>
{
private readonly List<int> images;
public int EventHandlerPosition { get; set; }
public EventHandler AddNewImageEventHandler { get; set; }
public ImageViewGridAdapter(Context context, int textViewResourceId, List<int> images)
: base(context, textViewResourceId, images)
{
this.images = images;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
ImageView v = (ImageView)convertView;
if (v == null)
{
LayoutInflater li = (LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);
v = (ImageView)li.Inflate(Resource.Layout.GridItem_Image, null);
// ** Need to assign event handler in here, since GetView
// is called an arbitrary # of times, and the += incrementor
// will result in multiple event fires
// Technique 1 - More flexisble, more maintenance ////////////////////
if (position == EventHandlerPosition)
v.Click += AddNewImageEventHandler;
// Technique 2 - less flexible, less maintenance /////////////////////
if (position == images.Count)
v.Click += AddNewImageEventHandler;
}
if (images[position] != null)
{
v.SetBackgroundResource(images[position]);
}
return v;
}
}
类ImageViewGridAdapter:ArrayAdapter
{
私有只读列表图像;
public int EventHandlerPosition{get;set;}
公共事件处理程序AddNewImageEventHandler{get;set;}
公共ImageViewGridAdapter(上下文上下文、int-textViewResourceId、列表图像)
:base(上下文、textViewResourceId、图像)
{
这个。图像=图像;
}
公共覆盖视图GetView(int位置、视图转换视图、视图组父视图)
{
ImageView v=(ImageView)convertView;
如果(v==null)
{
LayoutInflater li=(LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);
v=(ImageView)li.Inflate(Resource.Layout.GridItem_Image,null);
//**需要在此处分配事件处理程序,因为GetView
//被称为任意的#次,并且+=递增
//将导致多个事件火灾
//技术1-更灵活,更多维护////////////////////
如果(位置==EventHandlerPosition)
v、 单击+=AddNewImageEventHandler;
//技术2-灵活性更低,维护更少/////////////////////
if(position==images.Count)
v、 单击+=AddNewImageEventHandler;
}
如果(图像[位置]!=null)
{
v、 挫折资源(图像[位置]);
}
返回v;
}
}
然后,在将适配器分配给网格视图之前,只需分配这些值(位置不必在末尾,但对于页脚,应该是):
列表图像=新列表{
Resource.Drawable.image1、Resource.Drawable.image2、Resource.Drawable.image\u页脚
};
ImageViewGridAdapter recordAttachmentsAdapter=新的ImageViewGridAdapter(活动,0,图像);
recordAttachmentsAdapter.EventHandlerPosition=images.Count;
recordAttachmentsAdapter.AddNewImageEventHandler+=NewAttachmentClickHandler;
_recordAttachmentsGrid.Adapter=recordAttachmentsAdapter;
将其添加到适配器似乎很酷。但是footview肯定会比gridview中的其他项目更宽。有什么办法处理这个问题吗?在这种情况下,您必须使用自定义适配器。然后检查,如果position==items.length-1
,则膨胀一个更宽的视图。而其他行有3项。这怎么办?我明白了。然后,您可能必须使用网格视图
之外的视图GridView
不支持跨越。@Eric您找到解决问题的方法了吗?我需要添加一个单列标题“作为xy登录”,下面将是一个gridview,但第一个标题必须作为gridview滚动。
class ImageViewGridAdapter : ArrayAdapter<int>
{
private readonly List<int> images;
public int EventHandlerPosition { get; set; }
public EventHandler AddNewImageEventHandler { get; set; }
public ImageViewGridAdapter(Context context, int textViewResourceId, List<int> images)
: base(context, textViewResourceId, images)
{
this.images = images;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
ImageView v = (ImageView)convertView;
if (v == null)
{
LayoutInflater li = (LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);
v = (ImageView)li.Inflate(Resource.Layout.GridItem_Image, null);
// ** Need to assign event handler in here, since GetView
// is called an arbitrary # of times, and the += incrementor
// will result in multiple event fires
// Technique 1 - More flexisble, more maintenance ////////////////////
if (position == EventHandlerPosition)
v.Click += AddNewImageEventHandler;
// Technique 2 - less flexible, less maintenance /////////////////////
if (position == images.Count)
v.Click += AddNewImageEventHandler;
}
if (images[position] != null)
{
v.SetBackgroundResource(images[position]);
}
return v;
}
}
List<int> images = new List<int> {
Resource.Drawable.image1, Resource.Drawable.image2, Resource.Drawable.image_footer
};
ImageViewGridAdapter recordAttachmentsAdapter = new ImageViewGridAdapter(Activity, 0, images);
recordAttachmentsAdapter.EventHandlerPosition = images.Count;
recordAttachmentsAdapter.AddNewImageEventHandler += NewAttachmentClickHandler;
_recordAttachmentsGrid.Adapter = recordAttachmentsAdapter;