【Android】9.2 内置行视图的分类和呈现效果
分类:C#、Android、VS2015;
创建日期:2016-02-18
一、简介
Android内置了很多行视图模板,在应用程序中可直接使用这些内置的视图来呈现列表项。
要在ListView中使用内置的行视图呈现列表项,只需要通过Android.Resource.Layout类的属性指定资源的ID即可。例如:
public class MainActivity : Activity
{
……
protected override void OnCreate(Bundle bundle)
{
……
var listView1 = FindViewById<ListView>(Resource.Id.listView1);
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.TestListItem);
listView1.Adapter = adapter;
}
}
调用adapter对应的方法,可实现添加、删除、插入行的功能。例如:
adapter.Add("Hello");
二、运行截图
本示例演示如何利用继承自BaseAdapter<T>的类,用Android内置的行视图来呈现自定义的列表项。
在主界面中单击【例9-1 内置行视图的分类】,即进入该例子下面的导航界面:

选择某个内置视图的名称,即可在新的界面中呈现其外观效果。
1、TestListItem:带少量格式的单行文本项(行间距较小)。
2、SimpleListItem1:单行文本项(行间距较大)。

3、SimpleListItem2:双行文本项。
4、SimpleSelectableListItem:可多行选择的文本项。

5、SimpleListItemActivated1:单行文本项,与SimpleListItem1相似,但是它用背景色标识选择的项(可多选)。
6、SimpleListItemActivated2:双行文本项,与SimpleListItem2相似,但是它用背景色标识选择的项(可多选)。

7、SimpleListItemChecked:用“√”标识所选择的文本项(单选)。
8、SimpleListItemMultipleChoice:用复选框标识所选择的文本项(可多选)。

9、SimpleListItemSingleChoice:用单选按钮标识所选的文本项(单选)。
10、TwoLineListItem:双行文本项。

11、ActivityListItem:左侧可以带图像的单行文本项。
12、SelectDialogSingleChoice:对话框样式的单选按钮(字体较大)。

13、其他
除了上面列出的基本行视图以外,还有一些Android系统后来增加的内置视图,比如可折叠和展开的分组项(SimpleExpandableListItem1、SimpleExpandableListItem2)、……等,本项目包含所有章节示例的主界面就是利用可“折叠/展开”的内置视图实现的。
三、主要设计步骤
1、添加图片
先到Android SDK(API 23)的Samples文件夹下找到下面的图,然后将其拖放到项目的drawable文件夹下,并修改文件名添加前缀“ch09”,这样做的目的是为了方便区分是哪一章示例需要的图。

2、添加ch0901_BuildInViewsMain.axml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="单击某项观察呈现效果"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:gravity="center_horizontal"
android:paddingBottom="5dp" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1" />
</LinearLayout>
3、添加ch0901_BuildInViewsResult.axml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:gravity="center_horizontal"
android:paddingBottom="5dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#FF909090"
android:layout_marginBottom="5dp" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1" />
</LinearLayout>
4、添加ch0901MyBaseAdapter.cs文件
using System.Collections.Generic;
using Android.App;
using Android.Views;
using Android.Widget; namespace MyDemos.SrcDemos
{
public class ch0901TableItem
{
public string Heading { get; set; }
public string SubHeading { get; set; }
public int ImageResourceId { get; set; }
} public class ch0901MyBaseAdapter : BaseAdapter<ch0901TableItem>
{
private List<ch0901TableItem> items;
private Activity context;
private string demoTyoe;
public ch0901MyBaseAdapter(Activity context, List<ch0901TableItem> items, string demoTyoe)
{
this.context = context;
this.items = items;
this.demoTyoe = demoTyoe;
} public override ch0901TableItem this[int position]
{
get { return items[position]; }
} public override int Count
{
get { return items.Count; }
} public override long GetItemId(int position)
{
return position;
} public override View GetView(int position, View convertView, ViewGroup parent)
{
var listview1 = parent as ListView; //parent.FindViewById<ListView>(Resource.Id.listView1);
var item = items[position];
View view = null;
switch(demoTyoe)
{
case "(1)TestListItem":
view = context.LayoutInflater.Inflate(Android.Resource.Layout.TestListItem, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
break;
case "(2)SimpleListItem1":
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
break;
case "(3)SimpleListItem2":
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem2, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
view.FindViewById<TextView>(Android.Resource.Id.Text2).Text = item.SubHeading;
break;
case "(4)SimpleSelectableListItem":
listview1.ChoiceMode = ChoiceMode.Multiple;
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleSelectableListItem, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
break;
case "(5)SimpleListItemActivated1":
listview1.ChoiceMode = ChoiceMode.Multiple;
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItemActivated1, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
break;
case "(6)SimpleListItemActivated2":
listview1.ChoiceMode = ChoiceMode.Multiple;
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItemActivated2, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
view.FindViewById<TextView>(Android.Resource.Id.Text2).Text = item.SubHeading;
break;
case "(7)SimpleListItemChecked":
listview1.ChoiceMode = ChoiceMode.Single;
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItemChecked, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
break;
case "(8)SimpleListItemMultipleChoice":
listview1.ChoiceMode = ChoiceMode.Multiple;
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItemMultipleChoice, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
break;
case "(9)SimpleListItemSingleChoice":
listview1.ChoiceMode = ChoiceMode.Single;
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItemSingleChoice, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
break;
case "(10)TwoLineListItem":
view = context.LayoutInflater.Inflate(Android.Resource.Layout.TwoLineListItem, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
view.FindViewById<TextView>(Android.Resource.Id.Text2).Text = item.SubHeading;
break;
case "(11)ActivityListItem":
view = context.LayoutInflater.Inflate(Android.Resource.Layout.ActivityListItem, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
view.FindViewById<ImageView>(Android.Resource.Id.Icon).SetImageResource(item.ImageResourceId);
break;
case "(12)SelectDialogSingleChoice":
listview1.ChoiceMode = ChoiceMode.Single;
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SelectDialogSingleChoice, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.Heading;
break;
}
return view;
}
}
}
5、添加ch0901BuildInViewsResult.cs文件
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget; namespace MyDemos.SrcDemos
{
[Activity(Label = "【例9-1】内置行视图的分类")]
public class ch0901BuildInViewsResult : Activity
{
List<ch0901TableItem> items = new List<ch0901TableItem>();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ch0901_BuildInViewsResult); items.Add(new ch0901TableItem() { Heading = "蔬菜类(Vegetables)", SubHeading = "65项", ImageResourceId = Resource.Drawable.ch09Vegetables });
items.Add(new ch0901TableItem() { Heading = "水果类(Fruits)", SubHeading = "17项", ImageResourceId = Resource.Drawable.ch09Fruits });
items.Add(new ch0901TableItem() { Heading = "花蕾类(Flower Buds)", SubHeading = "5项", ImageResourceId = Resource.Drawable.ch09FlowerBuds });
items.Add(new ch0901TableItem() { Heading = "豆类(Legumes)", SubHeading = "33项", ImageResourceId = Resource.Drawable.ch09Legumes });
items.Add(new ch0901TableItem() { Heading = "圆疙瘩类(Bulbs)", SubHeading = "18项", ImageResourceId = Resource.Drawable.ch09Bulbs });
items.Add(new ch0901TableItem() { Heading = "块茎类(Tubers)", SubHeading = "43项", ImageResourceId = Resource.Drawable.ch09Tubers }); string demoType = Intent.GetStringExtra("demoType");
var textView1 = FindViewById<TextView>(Resource.Id.textView1);
textView1.Text= demoType;
ch0901MyBaseAdapter adapter = new ch0901MyBaseAdapter(this, items, demoType); var listView1 = FindViewById<ListView>(Resource.Id.listView1);
listView1.Adapter = adapter;
}
}
}
6、添加ch0901_BuildInViewsMain.axml文件
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget; namespace MyDemos.SrcDemos
{
[Activity(Label = "【例9-1】内置行视图的分类")]
public class ch0901BuildInViewsMain : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ch0901_BuildInViewsMain);
List<string> list = new List<string>()
{
"(1)TestListItem",
"(2)SimpleListItem1",
"(3)SimpleListItem2",
"(4)SimpleSelectableListItem",
"(5)SimpleListItemActivated1",
"(6)SimpleListItemActivated2",
"(7)SimpleListItemChecked",
"(8)SimpleListItemMultipleChoice",
"(9)SimpleListItemSingleChoice",
"(10)TwoLineListItem",
"(11)ActivityListItem",
"(12)SelectDialogSingleChoice",
};
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.TestListItem);
adapter.AddAll(list);
var listView1 = FindViewById<ListView>(Resource.Id.listView1);
listView1.Adapter = adapter;
listView1.ItemClick += (s, e) =>
{
Intent intent = new Intent(this,typeof(ch0901BuildInViewsResult));
intent.PutExtra("demoType", list[e.Position]);
StartActivity(intent);
};
}
}
}
【Android】9.2 内置行视图的分类和呈现效果的更多相关文章
- django关闭调试信息,打开内置错误视图
1 内置错误视图 Django内置处理HTTP错误的视图,主要错误及视图包括: 404错误:page not found视图 500错误:server error视图 400错误:bad reques ...
- 在Eclipse+ADT中开发Android系统的内置应用
转自: http://www.iteye.com/topic/1050439 在Eclipse+ADT中开发Android系统的内置应用 Android系统内置有:Browser(浏览器).Mms( ...
- 探讨Android中的内置浏览器和Chrome
1.Android默认浏览器和Chrome的区别 Android出厂自带的浏览器:安卓WebKit浏览器,也成内置浏览器或者默认浏览器. 安卓WebKit不是Chrome.Chrome浏览器在它的用户 ...
- 在Android中访问内置SE和基于SE的卡模拟(一)
2013-10-10 编写 前言 在“十问Android NFC手机上的卡模拟”文中仅仅简单的介绍了一下相关的概念,如果需要了解基于SE的卡模拟的更多细节,也就是,究竟在Android的NFC手机上, ...
- Android 操作手机内置存储卡中的文件
场景:需要读取指定文件的内容,此文件是手动存储到手机内置存储卡中的,且手机上不存在SD卡. 对于android通过activity提供的openFileOutput和openFileInput可以直接 ...
- Android开发中内置apk程序
首先申明,这里的方法介绍是针对我司自己项目中的具体开发板而做的. Mg701内置APK有三种方式 一. 这种方法必须要自己编写Android.mk文件(关于Android.mk可以参考 ...
- Day5 函数递归,匿名、内置行数,模块和包,开发规范
一.递归与二分法 一.递归 1.递归调用的定义 递归调用:在调用一个函数的过程中,直接或间接地调用了函数本身 2.递归分为两类:直接与间接 #直接 def func(): print('from fu ...
- android webview 添加内置对象
package com.android.EBrowser; import android.app.Activity;import android.graphics.Rect;import androi ...
- python内置函数大全(分类)
python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...
随机推荐
- 读取Style符号库样式的方法
以前进行符化的时候一般都是自定义Symbol,或者使用SymbologyControl进行选择,由于实际需要,我们来读取一下样式管理器中的样式.在ArcMap中打开如下:style下有很多样式类,每个 ...
- GET 和 POST的区别
1.最普遍的答案 GET使用URL或Cookie传参.而POST将数据放在BODY中. GET的URL会有长度上的限制,则POST的数据则可以非常大. POST比GET安全,因为数据在地址栏上不可见. ...
- 圆形Camera预览实现
需求 最近有个需求要求界面上使用圆形相机预览进行面部检测 , 具体需求如下图 关于Camera之前接触得比较多 , 主要就是通过SurfaceView显示预览视图 , 因此需要展示圆形预览界面, 只需 ...
- B/S与C/S的差别
前一段时间已经结束了C/S的学习,開始了B/S的旅程,那么为什么我们要学习这两个,这两个有什么差别呢?这些差别你知道多少呢? B/S结构.即Browser/Server(浏览器/server)结构.是 ...
- python解析发往本机的数据包示例 (解析数据包)
tcp.py # -*- coding: cp936 -*- import socket from struct import * from time import ctime,sleep from ...
- dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Conta
dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Cont ...
- 〖Linux〗Ubuntu13.10,配置tftp服务器
前言,配置了好久没有发现老是出问题tftp: server error: (2) Access violation,一般侦测之后... 1. 安装软件包:apt-getsudo apt-get ins ...
- 通过jdbc使用PreparedStatement,提升性能,防止sql注入
为什么要使用PreparedStatement? 一.通过PreparedStatement提升性能 Statement主要用于执行静态SQL语句,即内容固定不变的SQL语句.Statement每执行 ...
- RDD转换成DataFrames
官方提供了2种方法 1.利用反射来推断包含特定类型对象的RDD的schema.这种方法会简化代码并且在你已经知道schema的时候非常适用. 先创建一个bean类 case class Person( ...
- 基于FFmpeg的音频编码(PCM数据编码成AAC android)
概述 在Android上实现录音,并利用 FFmpeg将PCM数据编码成AAC. 详细 代码下载:http://www.demodashi.com/demo/10512.html 之前做的一个demo ...