简体   繁体   English

ListView和TextView子类

[英]ListView and TextView subclass

I'm an Android n00b and I'm learning how to code for the Android. 我是Android n00b,正在学习如何为Android编写代码。 I've created a subclass of TextView and I'd like to use it to create the cells for a ListView. 我创建了TextView的子类,我想用它来为ListView创建单元格。 How can I do that? 我怎样才能做到这一点?

Thanks! 谢谢!

You don't need to subclass it. 您无需对其进行子类化。 You just need to define them in a XML. 您只需要在XML中定义它们。 I think it's easier for you that are starting. 我认为开始对您来说比较容易。

For example, take a look at this guide . 例如,看一下本指南 It works "out of the box". 它“开箱即用”。 Just copy and paste the code there and study. 只需将代码复制并粘贴在那里并进行学习。

My very first custom list was based on it. 我的第一个自定义列表基于此。 It provides many TextViews etc. Later you may try ExpandableListView s... I saw this some days ago. 它提供了许多TextViews等以后你可以尝试ExpandableListView小号......我看到这个前几天。 Finally, when you practice a lot and get to learn it better, I would suggest that you try custom adapters to merge different levels/cells. 最后,当您练习很多并更好地学习它时,我建议您尝试使用自定义适配器来合并不同级别/单元。 Those are known as "headered lists". 这些被称为“标题列表”。 Very powerful, but to me kind of complex. 非常强大,但对我来说有点复杂。

A few days ago, I posted a question here on SO about headered lists. 几天前,我在这里在SO上发布了有关标题列表的问题。 I talked about how Jeff Sharkey (a Google employee, IIRC) managed to do it. 我谈到了Jeff Sharkey(IIRC的Google员工)如何做到这一点。 He made a very nice class. 他的课很好。 The people from CommonsWare also created a class for this, with the benefit of it being licensed under ASL 2 ("business-friendly"). 来自CommonsWare的人们还为此创建了一个类,这得益于ASL 2(“对企业友好”)的许可。 I talked about all that in my question. 我谈到了我所有的问题。 There is also a link to the Google groups where I solved my problem. 还有一个指向我解决问题的Google网上论坛的链接。 But since you're just starting, I would leave headered lists for later, of course. 但是,由于您才刚刚开始,所以我当然会留标题列表供以后使用。

Try reading the beginner guides I posted (the links for regular adapters), and also search Google for "custom listview". 尝试阅读我发布的入门指南(常规适配器的链接),然后在Google上搜索“自定义列表视图”。 Really, it has a lot more than we can offer here, and is readily available. 确实,它比我们在此提供的功能要多得多,并且随时可用。

Good luck! 祝好运!

You can refer your subclass in layout XML, you just need to make sure to use its full name. 您可以在布局XML中引用子类,只需要确保使用其全名即可。 If your subclass is com.example.CustomTextView you can refer to it in XML like this: 如果您的子类是com.example.CustomTextView,则可以使用XML来引用它,如下所示:

<com.example.CustomTextView/>

If you want to use that layout in a ListView create a custom adapter (subclass BaseAdapter, ArrayAdapter, CursorAdapter, or any other adapter) that uses it, just be sure to override the method that gets the view. 如果要在ListView中使用该布局,请创建一个使用该布局的自定义适配器(子类BaseAdapter,ArrayAdapter,CursorAdapter或任何其他适配器),只需确保重写获取视图的方法即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM