iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android列表实现(3)_自定义列表适配器思路及实现代码
  • 625
分享到

Android列表实现(3)_自定义列表适配器思路及实现代码

自定义Android 2022-06-06 11:06:09 625人浏览 八月长安
摘要

下面的例子为使用自定义的列表适配器来显示列表。 代码如下: View Code import Android.os.Bundle; import android.app.Li

下面的例子为使用自定义的列表适配器来显示列表。
代码如下:
View Code
import Android.os.Bundle;
import android.app.ListActivity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new SpeechListAdapter(this));
}

private class SpeechListAdapter extends BaseAdapter {
public SpeechListAdapter(Context context) {
mContext = context;
}

public int getCount() {
return mTitles.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

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;
}

private Context mContext;

private String[] mTitles =
{
"Henry IV (1)",
"Henry V",
"Henry VIII",
"Richard II",
"Richard III",
"Merchant of Venice",
"Othello",
"King Lear"
};

private String[] mDialogue =
{
"So shaken as we are, so wan with care," +
"Find we a time for frighted peace to pant," +
"And breathe short-winded accents of new broils" +
"To be commenced in strands afar remote." +
"No more the thirsty entrance of this soil" +
"Shall daub her lips with her own children's blood;" +
"Nor more shall trenching war channel her fields," +
"Nor bruise her flowerets with the armed hoofs" +
"Of hostile paces: those opposed eyes," +
"Which, like the meteors of a troubled heaven," +
"All of one nature, of one substance bred," +
"Did lately meet in the intestine shock" +
"And furious close of civil butchery" +
"Shall now, in mutual well-beseeming ranks," +
"March all one way and be no more opposed" +
"Against acquaintance, kindred and allies:" +
"The edge of war, like an ill-sheathed knife," +
"No more shall cut his master. Therefore, friends," +
"As far as to the sepulchre of Christ," +
"Whose soldier now, under whose blessed cross" +
"We are impressed and engaged to fight," +
"Forthwith a power of English shall we levy;" +
"Whose arms were moulded in their mothers' womb" +
"To chase these pagans in those holy fields" +
"Over whose acres walk'd those blessed feet" +
"Which fourteen hundred years aGo were nail'd" +
"For our advantage on the bitter cross." +
"But this our purpose now is twelve month old," +
"And bootless 'tis to tell you we will go:" +
"The refore we meet not now. Then let me hear" +
"Of you, my gentle cousin Westmoreland," +
"What yesternight our council did decree" +
"In forwarding this dear expedience.",
"Hear him but reason in divinity," +
"And all-admiring with an inward wish" +
"You would desire the king were made a prelate:" +
"Hear him debate of commonwealth affairs," +
"You would say it hath been all in all his study:" +
"List his discourse of war, and you shall hear" +
"A fearful battle render'd you in music:" +
"Turn him to any cause of policy," +
"The Gordian knot of it he will unloose," +
"Familiar as his garter: that, when he speaks," +
"The air, a charter'd libertine, is still," +
"And the mute wonder lurketh in men's ears," +
"To steal his sweet and honey'd sentences;" +
"So that the art and practic part of life" +
"Must be the mistress to this theoric:" +
"Which is a wonder how his grace should glean it," +
"Since his addiction was to courses vain," +
"His companies unletter'd, rude and shallow," +
"His hours fill'd up with riots, banquets, sports," +
"And never noted in him any study," +
"Any retirement, any sequestration" +
"From open haunts and popularity.",
"I come no more to make you laugh: things now," +
"That bear a weighty and a serious brow," +
"Sad, high, and working, full of state and woe," +
"Such noble scenes as draw the eye to flow," +
"We now present. Those that can pity, here" +
"May, if they think it well, let fall a tear;" +
"The subject will deserve it. Such as give" +
"Their money out of hope they may believe," +
"May here find truth too. Those that come to see" +
"Only a show or two, and so agree" +
"The play may pass, if they be still and willing," +
"I'll undertake may see away their shilling" +
"Richly in two short hours. Only they" +
"That come to hear a merry bawdy play," +
"A noise of targets, or to see a fellow" +
"In a long motley coat guarded with yellow," +
"Will be deceived; for, gentle hearers, know," +
"To rank our chosen truth with such a show" +
"As fool and fight is, beside forfeiting" +
"Our own brains, and the opiNIOn that we bring," +
"To make that only true we now intend," +
"Will leave us never an understanding friend." +
"Therefore, for goodness' sake, and as you are known" +
"The first and happiest hearers of the town," +
"Be sad, as we would make ye: think ye see" +
"The very persons of our noble story" +
"As they were living; think you see them great," +
"And follow'd with the general throng and sweat" +
"Of thousand friends; then in a moment, see" +
"How soon this mightiness meets misery:" +
"And, if you can be merry then, I'll say" +
"A man may weep upon his wedding-day.",
"First, heaven be the record to my speech!" +
"In the devotion of a subject's love," +
"Tendering the precious safety of my prince," +
"And free from other misbegotten hate," +
"Come I appellant to this princely presence." +
"Now, Thomas Mowbray, do I turn to thee," +
"And mark my greeting well; for what I speak" +
"My body shall make good upon this earth," +
"Or my divine soul answer it in heaven." +
"Thou art a traitor and a miscreant," +
"Too good to be so and too bad to live," +
"Since the more fair and crystal is the sky," +
"The uglier seem the clouds that in it fly." +
"Once more, the more to aggravate the note," +
"With a foul traitor's name stuff I thy throat;" +
"And wish, so please my sovereign, ere I move," +
"What my tongue speaks my right drawn sWord may prove.",
"Now is the winter of our discontent" +
"Made glorious summer by this sun of York;" +
"And all the clouds that lour'd upon our house" +
"In the deep bosom of the ocean buried." +
"Now are our brows bound with victorious wreaths;" +
"Our bruised arms hung up for monuments;" +
"Our stern alarums changed to merry meetings," +
"Our dr eadful marches to delightful measures." +
"Grim-visaged war hath smooth'd his wrinkled front;" +
"And now, instead of mounting barded steeds" +
"To fright the souls of fearful adversaries," +
"He capers nimbly in a lady's chamber" +
"To the lascivious pleasing of a lute." +
"But I, that am not shaped for sportive tricks," +
"Nor made to court an amorous looking-glass;" +
"I, that am rudely stamp'd, and want love's majesty" +
"To strut before a wanton ambling nymph;" +
"I, that am curtail'd of this fair proportion," +
"Cheated of feature by dissembling nature," +
"DefORMed, unfinish'd, sent before my time" +
"Into this breathing world, scarce half made up," +
"And that so lamely and unfashionable" +
"That dogs bark at me as I halt by them;" +
"Why, I, in this weak piping time of peace," +
"Have no delight to pass away the time," +
"Unless to spy my shadow in the sun" +
"And descant on mine own deformity:" +
"And therefore, since I cannot prove a lover," +
"To entertain these fair well-spoken days," +
"I am determined to prove a villain" +
"And hate the idle pleasures of these days." +
"Plots have I laid, inductions dangerous," +
"By drunken prophecies, libels and dreams," +
"To set my brother Clarence and the king" +
"In deadly hate the one against the other:" +
"And if King Edward be as true and just" +
"As I am subtle, false and treacherous," +
"This day should Clarence closely be mew'd up," +
"About a prophecy, which says that 'G'" +
"Of Edward's heirs the murderer shall be." +
"Dive, thoughts, down to my soul: here" +
"Clarence comes.",
"To bait fish withal: if it will feed nothing else," +
"it will feed my revenge. He hath disgraced me, and" +
"hindered me half a million; laughed at my losses," +
"mocked at my gains, scorned my nation, thwarted my" +
"bargains, cooled my friends, heated mine" +
"enemies; and what's his reason? I am a Jew. Hath" +
"not a Jew eyes? hath not a Jew hands, organs," +
"dimensions, senses, affections, passions? fed with" +
"the same food, hurt with the same weapons, subject" +
"to the same diseases, healed by the same means," +
"warmed and cooled by the same winter and summer, as" +
"a Christian is? If you prick us, do we not bleed?" +
"if you tickle us, do we not laugh? if you poison" +
"us, do we not die? and if you wrong us, shall we not" +
"revenge? If we are like you in the rest, we will" +
"resemble you in that. If a Jew wrong a Christian," +
"what is his humility? Revenge. If a Christian" +
"wrong a Jew, what should his sufferance be by" +
"Christian example? Why, revenge. The villany you" +
"teach me, I will execute, and it shall go hard but I" +
"will better the instruction.",
"Virtue! a fig! 'tis in ourselves that we are thus" +
"or thus. Our bodies are our gardens, to the which" +
"our wills are gardeners: so that if we will plant" +
"nettles, or sow lettuce, set hyssop and weed up" +
"thyme, supply it with one gender of herbs, or" +
"distract it with many, either to have it sterile" +
"with idleness, or manured with industry, why, the" +
"power and corrigible authority of this lies in our" +
"wills. If the balance of our lives had not one" +
"scale of reason to poise another of sensuality, the" +
"blood and baseness of our natures would conduct us" +
"to most preposterous conclusions: but we have" +
"reason to cool our raging motions, our carnal" +
"stings, our unbitted lusts, whereof I take this that" +
"you call love to be a sect or scion.",
"Blow, winds, and crack your cheeks! rage! blow!" +
"You cataracts and hurricanoes, spout" +
"Till you have drench'd our steeples, drown'd the cocks!" +
"You sulphurous and thought-executing fires," +
"Vaunt-couriers to oak-cleaving thunderbolts," +
"Singe my white head! And thou, all-shaking thunder," +
"Smite flat the thick rotundity o ' the world!" +
"Crack nature's moulds, an germens spill at once," +
"That make ingrateful man!"
};
}

private class SpeechView extends LinearLayout {
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 void setTitle(String title) {
mTitle.setText(title);
}

public void setDialogue(String words) {
mDialogue.setText(words);
}
private TextView mTitle;
private TextView mDialogue;
}
}

该例子中自定义了适配器 SpeechListAdapter (其继承父类BaseAdapter)以及作为记录模板的 SpeechView(其父类为LinearLayout)。
SpeechListAdapter中的方法getView实现了返回指定位置的记录显示对象的逻辑。 您可能感兴趣的文章:Android ListView万能适配器实例代码Android之自定义实现BaseAdapter(通用适配器二)Android之自定义实现BaseAdapter(通用适配器一)学习Android自定义Spinner适配器举例讲解Android应用中SimpleAdapter简单适配器的使用Android编程中常用适配器及自定义适配器用法实例分析Android中 自定义数据绑定适配器BaseAdapter的方法Android控件系列之相册Gallery&Adapter适配器入门&控件缩放动画入门Android 适配器模式应用及设计原理


--结束END--

本文标题: Android列表实现(3)_自定义列表适配器思路及实现代码

本文链接: https://www.lsjlt.com/news/28051.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • html如何实现自定义列表
    小编给大家分享一下html如何实现自定义列表,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!定义列表不仅仅是一列项目,而是项目及其注释的组合。列表以 <dl&...
    99+
    2023-06-17
  • Android自定义view实现列表内左滑删除Item
    目录前言需求运行效果编写代码主要问题动态生成TextView将TextView对齐到当前容器右端滑动出界问题滑动开始判定后续订正前言 上一篇文章自定义了一个左滑删除的Recycler...
    99+
    2023-02-09
    Android列表内左滑删除Item Android自定义view左滑删除Item
  • Android如何实现自定义单选多选下拉列表
    这篇文章主要为大家展示了“Android如何实现自定义单选多选下拉列表”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Android如何实现自定义单选多选下拉列表”这篇文章吧。直接上效果:实现方案...
    99+
    2023-05-30
    android
  • vue自定义表格列的实现过程记录
    目录前言效果图 setTable组件 使用 结束语 前言 在我们开发PC端的项目使用表单时,尤其是crm系统,应该经常会遇到这样的需求, 用户需要根据设置来自定义显示列。 查了ele...
    99+
    2024-04-02
  • ​CSS如何实现表格列宽自适用
    这篇文章主要为大家展示了“CSS如何实现表格列宽自适用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“CSS如何实现表格列宽自适用”这篇文章吧。CSS:表格列宽自...
    99+
    2024-04-02
  • Python代码实现列表分组计数
    目录1. count_by2. 使用字典推导式3. 使用collections.defaultdict简化代码 本篇阅读的代码片段来自于30-seconds-of-python。 1...
    99+
    2024-04-02
  • Android自定义RadioGroupX实现多行多列布局
    前言 今天在做新需求的时候,活动有多个类型可以选择,UI给的设计图为多行多列排版,且单项选择,细细想来,谷歌并没有为我们提供类似的控件,初步设想使用RecyclerView实现多行多...
    99+
    2024-04-02
  • Android实现Ant Design 自定义表单组件
    Ant Design 组件提供了Input,InputNumber,Radio,Select,uplod等表单组件,但实际开发中这是不能满足需求,同时我们希望可以继续使用Form提供的验证和提示等方法(使用起来确实很爽),这时需要...
    99+
    2023-05-31
    ant design 表单组件
  • mybatis插件实现自定义改写表名实例代码
    代码如下: @Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatem...
    99+
    2024-04-02
  • vue 长列表数据刷新的实现及思考
    目录开篇一、效果展示二、代码开篇 通过 vue 进行列表展示的时候如果数据太多可能会卡顿,这里通过滑动计算只创建跟刷新可见部分 dom 元素,这里仅仅代表着复用思路 一、效果展示 ...
    99+
    2023-05-14
    vue 长列表数据刷新 vue 数据刷新
  • Android 使用RecycleView列表实现加载更多的示例代码
    目录1.界面布局2.功能实现 1.界面布局 <?xml version="1.0" encoding="utf-8"?> <FrameLayou...
    99+
    2024-04-02
  • React实现一个高度自适应的虚拟列表
    目录改造前:改造后:0x0 基础知识 0x1 实现一个“定高”虚拟列表 原理: 优化: 0x2 列表元素高度自适应 近期在某平台开发迭代的过程中遇到了超长List嵌套在antd Mo...
    99+
    2024-04-02
  • Python 代码实现列表的最小公倍数
    目录1. lcm2. functools.reduce3. 实际使用本篇阅读的代码片段来自于30-seconds-of-python(https://github.com/30-se...
    99+
    2024-04-02
  • CSS如何实现表格首行首列固定和自适应窗口
    这篇文章主要讲解了“CSS如何实现表格首行首列固定和自适应窗口”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“CSS如何实现表格首行首列固定和自适应窗口”吧!先了解几个概念:table-lay...
    99+
    2023-06-08
  • SpringBoot自定义starter启动器的实现思路
    目录一.引言二. 需求说明三. 设计思路四. 实现步骤1. Step1 业务定义2. Step2 自动配置2.1 HelloService类2.2 HelloProperties类2...
    99+
    2022-11-13
    SpringBoot自定义starter启动器 SpringBoot自定义starter
  • Python代码然后实现列表分组计数
    本篇文章给大家分享的是有关Python代码然后实现列表分组计数,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1. count_bydef count_by(arr,&...
    99+
    2023-06-25
  • Android自定义view实现雪花特效实例代码
    目录一、前言二、创意名三、效果展示四、实现步骤五、编码实现总结一、前言 这个冬天,老家一直没有下雨, 正好圣诞节,就想着制作一个下雪的特效。 圣诞祝福:平安夜,舞翩阡。雪花飘,飞满天...
    99+
    2022-12-28
    android实现雪花特效 android雪花特效 android自定义view
  • Redis中如何实现自定义序列化器
    在Redis中实现自定义序列化器需要使用Redis的自定义模块功能。Redis的自定义模块功能允许用户编写自定义的功能模块,并在Re...
    99+
    2024-04-29
    Redis
  • 如何实现axios的自定义适配器adapter
    目录1. 适配器要实现的功能1.1 promise 和工具1.2 响应的格式1.3 超时设置1.4 主动取消请求2. 编写自定义适配器3. 将适配器添加到 axios 中4. 总结1...
    99+
    2024-04-02
  • Android开发自定义实时图表控件实现示例
    目录概述演示环境实现第一步:新建项目RealTimeChartDemo第二步:新建RealTimeChart类第三步:添加自定义变量第四步:初始化基础参数第五步:初始化宽高等参数第六...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作