展会信息港展会大全

Android ApiDemos示例解析(171):Views->Lists->4. ListAdapter
来源:互联网   发布日期:2015-10-02 16:20:47   浏览:1766次  

导读:上一篇:http://www.2cto.com/kf/201209/156201.html本例返回一自定义View -SpeechView. 这个View由两个TextView构成,一个Te...

上一篇:http://www.2cto.com/kf/201209/156201.html

本例返回一自定义View -SpeechView. 这个View由两个TextView构成,一个TextView显示Title,一个View显示内容:其构造函数定义如下:

[java]

public SpeechView(Context context, String title, String words) {

super(context);

this.setOrientation(VERTICAL);

// Here we build the child views in code. They could also have

// been specified in an XML file.

mTitle = new TextView(context);

mTitle.setText(title);

addView(mTitle, new LinearLayout.LayoutParams(

LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

mDialogue = new TextView(context);

mDialogue.setText(words);

addView(mDialogue, new LinearLayout.LayoutParams(

LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

}

public SpeechView(Context context, String title, String words) {

super(context);

this.setOrientation(VERTICAL);

// Here we build the child views in code. They could also have

// been specified in an XML file.

mTitle = new TextView(context);

mTitle.setText(title);

addView(mTitle, new LinearLayout.LayoutParams(

LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

mDialogue = new TextView(context);

mDialogue.setText(words);

addView(mDialogue, new LinearLayout.LayoutParams(

LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

}

自定义的SpeechListAdapter 的getView定义如下:

[java]

public View getView(int position, View convertView,

ViewGroup parent) {

SpeechView sv;

if (convertView == null) {

sv = new SpeechView(mContext, mTitles[position],

mDialogue[position]);

} else {

sv = (SpeechView) convertView;

sv.setTitle(mTitles[position]);

sv.setDialogue(mDialogue[position]);

}

return sv;

}

public View getView(int position, View convertView,

ViewGroup parent) {

SpeechView sv;

if (convertView == null) {

sv = new SpeechView(mContext, mTitles[position],

mDialogue[position]);

} else {

sv = (SpeechView) convertView;

sv.setTitle(mTitles[position]);

sv.setDialogue(mDialogue[position]);

}

return sv;

}

因此列表的每项都显示为一个SpeechView:

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港