android - bindView and newView for two view layout in CursorAdapter

Deadly 提交于 2019-12-01 23:56:49

问题


So here's the story:

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?

I'm thinking of something like this:

@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);
    }
}

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()
    }
}

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.


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/14557251/android-bindview-and-newview-for-two-view-layout-in-cursoradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!