列表视图中项目的不同布局
我有一些扩展的光标适配器,我用列表中的项目的上下文和资源布局来调用 super ,类似这样。
在我的适配器中调用 super:
super(activity, viewResource, c, false);
创建我的适配器:
new MyCursorAdapter(this, null, R.layout.my_list_item, null);
我想要实现的就像我用油漆制作的愚蠢模型。 换句话说,我希望项目具有不同类型的布局,例如,我希望所有偶数项目具有布局 1,所有奇数项目具有布局 2。到目前为止,在本例中我只能给出一种布局 R.layout.my_list_item。是否可以动态更改布局? 是否可以构建适配器以具有不同布局的项目?我的目标是动态选择项目的布局。我不想为我想要的所有项目只有一种布局,例如两个...
谢谢
I have some extended cursor adapter, in witch I call super with the context and the resource layout for the item in the list, something like this.
call to super in my adapter:
super(activity, viewResource, c, false);
creation of my adapter:
new MyCursorAdapter(this, null, R.layout.my_list_item, null);
What I want to achieve is something like my stupid mock up made in paint.
Put into words I want to have different kinds of layout for the items, for example I want all even items to have layout1 and all odd to have the layout2. So far I am able to give only one layout in this case R.layout.my_list_item. Is it possible to dynamically change the layout ?
Is it possible to construct the adapter to have items with different layout ? My goal is to dynamically chose the layout of the item. I do not want to have just one layout for all of the items I want to have foe example two...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,但你必须做两件事。首先,重写适配器中的
getItemViewType()
方法,以便确保您的bindView()
仅获取适合列表中特定位置的视图,如下所示:完成此操作后,只需在
bindView()
中进行一个简单的测试,检查它应该收到什么类型的视图,并相应地进行设置,如下所示:请注意,您将拥有必须采用不同的方法correpondsToViewType,一种采用光标,另一种采用 int(用于位置)。这些的实现将根据您想要执行的操作而有所不同。
请注意,以这种方式执行操作将允许您重用可能回收的视图。如果您不这样做,您的应用将会遭受巨大性能损失。滚动会非常不稳定。
Yes, you're going to have to do two things though. First, override the
getItemViewType()
method in your adapter so that you can be sure yourbindView()
only get's views that appropriate for a particular position in the list, like so:Once you do that, just have a simple test in your
bindView()
that checks to see what type of view it should have recieved and setup things accordingly like so:Note that you're going to have to have to different methods for correpondsToViewType, one that takes a cursor and one that takes an int (for a position). The implementation for these will vary depending on what you want to do.
Note that doing things this way will allow you to reuse potentially recycled views. If you don't do this, your app is going to take a huge performance hit. Scrolling will be super choppy.
我猜测您从自定义适配器的名称中扩展了 SimpleCursorAdapter 。您将需要重写函数 getView 在适配器中,并根据列表中的对象膨胀不同的布局并返回该视图。
EX:
这只是一个示例,有点 sudo 代码,因此需要根据您的具体情况进行一些调整。
I'm guessing your extending SimpleCursorAdapter from the name of your custom adapter. You will want to override the function getView in your adapter and depending on the object in the list inflate a different layout and return that view.
EX:
This is just an example and is somewhat sudo code so it will need some adjustments for your specific situation.
只需重写 newView 方法:
Just override the newView Method: