问题描述
背景:我想在我的应用程序来实现信使系统,和我正在写一个自定义的的CursorAdapter
显示的邮件在的ListView
在聊天窗口中。我想使用的传入和传出消息(即光标保存在SQLite的行中的信息)不同行的布局。每行中有相同的元件具有相同的ID,但它们的排列不同。
Background: I'm trying to implement a messenging system in my app, and I'm writing a custom CursorAdapter
to display the messages in a ListView
in the chat window. I want to use a different row layout for incoming and outgoing messages (information that is saved in the SQLite row in the cursor). Each row has the same elements in it with the same IDs, but they are arranged differently.
问题:目前,我已经重写 NewView的()
和 bindView()
。当的ListView
首先填充,它创建的所有查看
取值完美,检查每一行,看看它要么传入或传出,和充气适当的XML文件。然而,当我滚动,或一个新的消息被添加到窗口,适配器回收查看
S为错了行。我会重写 getView()
,但它不是通过光标
作为参数,所以我也没办法的了解该行是否应该传入或传出。
The Problem: Currently, I have overridden newView()
and bindView()
. When the ListView
is first populated, it creates all of the View
s perfectly, checking each row to see if it's either incoming or outgoing, and inflating the proper XML file. However, when I scroll or a new message is added to the window, the adapter recycles View
s for the wrong rows. I would override getView()
, but it is not passed the Cursor
as a parameter, so I have no way of knowing whether the row should be incoming or outgoing.
我不是找code,而是一些建议一个优雅的实现。在此先感谢!
I'm not looking for code, but rather, some suggestion for an elegant implementation. Thanks in advance!
推荐答案
下面是两种可能的解决方案:
Here are two possible solutions:
(1)使用单一的布局。最直接的办法就是正好有根视图是它包含N个孩子每个不同状态的的FrameLayout,而你让他们中的一个可见的和有约束力的,当其他人都走了。当然,你要小心不要让这导致你的项目中所包含的意见数量爆炸。
(1) Use a single layout for all items, which you can adjust when binding to show as desired. The most straight-forward way would just to have the root view be a FrameLayout which contains N children for each of the different states, and you make one of them visible and all others gone when binding. Of course you want to take care to not let this cause your items to explode in the number of views they contain.
(2)实施Adapter.getItemViewType()http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)讲述了不同类型的项目你有那么这将回收正确的列表视图。
(2) Implement Adapter.getItemViewType() http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int) to tell the list view about the different types of items you have so it will recycle the correct one.
这篇关于我将如何使用基于游标数据定制CursorAdapter的不同行布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!