3

所以这里的故事:

我想在我的适配器中使用两种布局。所以基本上,我需要在 newView() 中有一个 if 来确定要返回哪个视图,并在 bindView() 中有一个 if 来知道在视图中做什么。这是正确的方法吗?

我在想这样的事情:

@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上:

@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() 在 bindView 中的 c.getPosition() 上给出了不同的结果。我实际上只是在考虑重写 getView() 但他们说好的做法是在 CursorAdapter 中重写 newView 和 bindView。

4

1 回答 1

0

除了我对糟糕的数据库设计的评论(关于这个问题)......

我会制作一个包含header和布局的content布局,然后header将两个布局的类型标记为visiblity="gone". 对于需要子视图的行bindView和行,使其和内容 subview 。当然,当您想要内容布局时交换它们。newViewheaderView.VISIBLEView.GONE

根据我的评论,您可能想要ExpandableListView. 我没有关于它的统计或测量,但我怀疑性能会更好。这也意味着您不会依赖将“标题”与您的数据放在同一个表中的 hacky 解决方案。

于 2013-11-17T21:06:08.867 回答