设为首页 加入收藏

TOP

C#-Xamarin的Android项目开发(二)——控件应用(二)
2019-09-01 23:13:11 】 浏览:61
Tags:C#-Xamarin Android 项目开发 控件 应用
tDrawable(Resource.Drawable.home_button_search); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 2: { text.Text = "checkin"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_checkin); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 3: { text.Text = "promo"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_promo); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 4: { text.Text = "tuan"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_tuan); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 5: { text.Text = "rank"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_rank); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 6: { text.Text = "history"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_history); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 7: { text.Text = "myzone"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_myzone); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } case 8: { text.Text = "more"; Drawable draw = this.resources.GetDrawable(Resource.Drawable.home_button_more); draw.SetBounds(0, 0, draw.IntrinsicWidth, draw.IntrinsicHeight); text.SetCompoundDrawables(null, draw, null, null); break; } } paramView.SetMinimumHeight((int)(96.0F * localDisplayMetrics.Density)); paramView.SetMinimumWidth(((-12 + localDisplayMetrics.WidthPixels) / 3)); return paramView; } }

代码如上所示,适配器的构造函数接受了一个参数,是适配器所属Activity,主要用于在适配器里调用Activy的信息。

然后我们通过LayoutInflater(布局填充类),将xml布局文件实例化为它对应的View对象,以供后续使用。

然后我们重写BaseAdapter类的一些属性和方法。

其中重写的Count属性需要特别注意,他代表我们列表的显示数,他是需要赋值的。这里的事例为其定义了一个常数9。

接下来我们重点看下GetView方法。

GetView这个方法干了很多事,作为C#开发者,从字面上是很难理解它是干什么的;不过我们可以联想思考,我们暂时把他理解为行的导入事件,这样就很形象了吧。

因为,至于为什么会叫GetView,我想,大概是因为他即干了行绑定数据的事,又干了行视图布局的事,所以没有更合适的命名,才这么叫的吧。

这也是为什么我感觉他奇葩的原因,因为在之前的Activity和布局中已经混淆了视图和数据,然后,在控件里,我们又一次把数据和布局搅和在了一起。。。。

下面我们看看它是如何混淆,不,他是如何工作的吧。

首先,在行导入的GetView中,我们找到要填充的布局XML——activity_label_item.xml。

paramView = inflater.Inflate(Resource.Layout.activity_label_item, null);

接着,我们找这个行布局内的控件,然后为他赋值,这里activity_label_item.xml很简单,只有一个Textview,也就是说,这里我们需要做的就是给他赋值。

然后,我们通过paramInt来判断当前行,正常情况,在这里找到Activity的数据集合,找到集合的对应行赋值即可了。

Demo里我们做了一下特殊处理,我们为行视图添加了图片。

运行结果如下图:

如图所示,列表已经创建完成了。

下面我们为列表添加点击事件;代码如下:

my_grid.ItemClick += (s, e) =>
{
    this.ShowToast("Click Me" + e.Id);
};

代码很简单,也很简洁,实现效果如下:

如上图所示,我们成功的实现了点击事件。

到此,

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇阿里云对象存储OSS访问控制 下一篇google zxing android扫描优化&解..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目