Android的 - bindView和NewView的在CursorAdapter的两个视点布局 [英] android - bindView and newView for two view layout in CursorAdapter

查看:166
本文介绍了Android的 - bindView和NewView的在CursorAdapter的两个视点布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,这里的故事:

So here's the story:

我想在我的适配器使用两种布局。所以基本上,我需要有一个,如果在NewView的(),以确定返回的视图和以及如果bindView()知道,以及如何在视图中做的。这是正确的做法?

I want to use two layouts in my adapter. So basically, I need to have an if in the newView() to determine which view to return and and an if in bindView() to know as well what to do in the view. Is this the right approach?

我在想是这样的:

@Override
public View newView(Context context, Cursor c,
        ViewGroup parent) {     
    if (HEADER == getItemViewType(c.getPosition())){
        return (View) layoutInflater.inflate(R.layout.my_header, null);
    } else {
        return (View) layoutInflater.inflate(R.layout.my_row, null);
    }
}

然后在bindView:

Then on bindView:

@Override
public void bindView(final View view, final Context context,
        Cursor c) {     
    if (TYPE_HEADER == getItemViewType(c.getPosition())){
        // init and set values here e.g. view.findViewById().setText()
    } else {
        // init and set values here e.g. view.findViewById().setText()
    }
}

我在这里在正确的轨道上?因为根据我的日志,在NewView的的c.getPosition()给出了c.getPosition()在bindView不同的结果。实际上,我想只是重写getView(),但他们说好的做法是压倒一切的NewView的和bindView在CursorAdapter的。

Am I on the right track here? Because according to my logs, the c.getPosition() in newView gives different result on c.getPosition() in bindView. I'm actually thinking of just overriding the getView() but they said good practice is overriding newView and bindView in CursorAdapter.

推荐答案

除了我的评论(对这个问题)约差数据库设计...

Aside from my comment (on the question) about poor database design...

我会做一个布局包两个报头内容布局,然后标记可见性=水涨船高。对于这两种 bindView NewView的在您需要的标题行子视图,使其 View.VISIBLE 和内容子视图 View.GONE 。当然,当你想要的内容布局交换它们。

I would make one layout that wraps both of the header and content layouts, then mark the header type of the two layouts as visiblity="gone". For both bindView and newView rows where you need the header subview, make it View.VISIBLE and the content subview View.GONE. Of course when you want the content layout swap them.

按我的意见,你可能想 ExpandableListView 。我没有它的统计数据或测量,但我怀疑的表现会更好。这也将意味着你不是靠干扰头到同一个表作为您的数据哈克的解决方案。

As per my comment, you probably want ExpandableListView. I don't have statistics or measurements on it, but I suspect that the performance will be better. It will also mean that you aren't relying on the hacky solution of jamming "headers" into the same table as your data.

这篇关于Android的 - bindView和NewView的在CursorAdapter的两个视点布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆