Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 11 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
我记得学习 ArrayAdapters 并不容易。互联网上有很多教程。
扩展 ArrayAdapter 您可能需要重写方法。
getCount() - 列表中的项目数。
public int getViewTypeCount() - ArrayList 呈现数据的方式数量。通常这是 1。但是如果您的列表中有多种类型的数据并且具有多个视图,那么您必须更改此数字。一个例子是联系人列表。它有 2 种类型。 1 代表联系人本身,另一个是您看到的 A、B 等字母类别。
getItemViewType(position) - 对于职位,它应该获取什么类型?通常,1.除非您有多种类型。
public long getItemId(intposition) - 您要呈现的项目类型。
真正重要的一个!
public View getView(intposition,ViewconvertView,ViewGroupparent)
回收。如果一个项目
滚动离开屏幕,它的视图是
持有并等待被重用
另一个项目将出现在
屏幕。这有助于提高性能,因为
你只会有大约8个左右
屏幕上的相同视图最多
最初convertView将为null
因为它不能重用任何东西,除了作为
用户在屏幕上滚动
得到他们。 ViewGroup 父级我不是这样
肯定超出了在视图中使用它的范围
通货膨胀。
下面是 getView 使用的示例
ViewHolder 模式。视图持有者
有助于使您的滚动更加流畅
因为你不必重做这一切
findViewById 的东西一遍又一遍
再次。每次更新视图时
与新信息一起使用
holder.WidgetName 来做到这一点。
}
I remember learning ArrayAdapters and it wasn't easy. There are plenty of tutorials on the internet.
Extending ArrayAdapter you'll probably want to override methods.
getCount() - Number of items in your list.
public int getViewTypeCount() - Number of ways data will be presented by the ArrayList. Usually this is 1. But if your list has multiple types of data in it that has multiple views then you'll have to change this number. An Example is the contacts list. It has 2 types. 1 for the contact itself and the other is the letter category that you see for A's, B's, etc.
getItemViewType(position) - For the position, what type should it get? Usually, 1. Unless you have multiple types.
public long getItemId(int position) - The type of item you're going to present.
The really important one!
public View getView(int position, View convertView, ViewGroup parent)
recycling. What that means if an item
scrolls off the screen, it's view is
held and waiting to be reused by
another item that will come on the
screen. This helps performance since
you'll only have about 8 or so of the
same view on the screen max.
Initially convertView will be null
since it can't reuse anything but as
the user scrolls on the screen you'll
get them. ViewGroup parent I'm not so
sure beyond using it in view
inflation.
Below is an example of getView using
the ViewHolder pattern. ViewHolder
helps makes your scrolling smooth
since you don't have to redo all that
findViewById stuff over and over
again. Each time you update the view
with the new information use the
holder.WidgetName to do it.
}
现在我没有任何参考可以给您,但这就是您可能会做的事情来获得您想要的:
您可能在 XML 中拥有 ListView,因此在代码中实例化一个 ListView 对象:
一旦您获得参考为此,创建一个扩展 BaseAdapter 的自定义类。
一个好主意是将此类放入您的 Activity 类中,以便它可以访问您的 Activity 保存的所有数据。
在扩展 BaseAdapter 时,您需要执行一些操作才能正常工作。
我将在下面解释所有这些,但实现 BaseAdapter 的 getView() 方法是最重要的。
每次 ListView 绘制一行时,运行时系统都会调用此方法。
因此,您应该在该方法中完成所有您希望在单行中完成的操作。
找到下面的代码:
希望它对您有帮助。
请记住在单独的布局文件中创建行的布局。
如果您想更深入地了解,请尝试 CommonsGuy 网站上的此链接。这是他很棒的书的摘录,专门讨论 ListView 自定义适配器
编辑:这是我关于它的博客文章以及一个示例项目:http://thetechnib.blogspot.com/2010/12/android-tutorial-custom-adapter-for.html
Right now I am not having any reference to give you, but this is what you might be doing to get what you want:
You might have the ListView in the XML, so instantiate a ListView object in your code:
As soon as you get reference to it, create a custom class that extends BaseAdapter.
A good idea will be to put this class inside your Activity class so that it can access all the data that your Activity holds.
While extending BaseAdapter, there are some things that you need to do in order to get things working.
I am explaining all of them below, but implementing the getView() method of the BaseAdapter is the most important thing.
This method will be called by the runtime system everytime the ListView draws a row.
So you should be doing all inside this method, that you would want to be done for a single row.
Find the code below:
Hope it helped you.
Remember to create the layout of the row in a separate layout file.
If you're trying to get deeper, try this link at CommonsGuy's website. It is an excerpt to his awesome book, that specifically deals with the ListView custom adapters
EDIT: here's my blog post about it and a sample project too: http://thetechnib.blogspot.com/2010/12/android-tutorial-custom-adapter-for.html