开发者

different views on a single list ( Android )

Is there a way to make a list with different views? I mean that row in posiotion开发者_JS百科 X will have layout "X.xml" and row number Z will have layout "Y.xml"?

I've tried to manipulate ListRowAdapter() and getView() in a few ways but with no success...


Hi, Is there a way to make a list with different views? I mean that row in posiotion X will have layout "X.xml" and row number Z will have layout "Y.xml"?

Sure.

Step #1: Create an Adapter class, by extending BaseAdapter, ArrayAdapter, CursorAdapter, etc.

Step #2: Implement getViewTypeCount() to return how many different row types there are

Step #3: Implement getItemViewType() to return a number between 0 and the value returned by getViewTypeCount(), indicating which row type a given position will use

Step #4: Override getView() (or newView() and bindView() for CursorAdapter) and have it create the right row


I don't think there is an easy way to do it out of the box. Depending on how different your layouts are, you might be able to come up with a single layout, and then show or hide elements of it in getView depending on the row. If you set an item to Layout.GONE, its as if it isn't there.


I solved it in another way: in the ListRowAdapter -> getView() I've made an if() statement on the inflater:

LayoutInflater inflater=context.getLayoutInflater();
View row=inflater.inflate(R.layout.X, null);
if(somthing)
{
row=inflater.inflate(R.layout.Y, null);
//whatever
}
and in the end: return row;

works like a charm :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜