Odd number ListView

2019-06-07 05:12发布

I'm trying to make a ListView with the situation displayed here: enter image description here

The ListView items will be loaded from a database so how many items are in it depends on the data returned from the database. But anyways, if it's 1 item or a million, the same pattern should be repeated. I'm absolutely clueless how to make a layout like this. Can anyone give an example, a link to another Stack Overflow question (Since I've no clue what this would be called to look it up myself..) similar to this question or a tutorial? I hope someone can help me! Thanks in advance.

3条回答
老娘就宠你
2楼-- · 2019-06-07 05:33

Read this article RecyclerView: Grid with header. You can achieve described behavior using GridLayoutManager with 2 columns and GridLayoutManager.setSpanSizeLookup method.

查看更多
唯我独甜
3楼-- · 2019-06-07 05:43

I think you have to use the RecycleView. Adapter have method to set different row view.

@Override
public int getItemViewType(int position) {
    // Define a way to determine which layout to use, here it's just evens and odds.
    return position % 2;
}

@Override
public int getViewTypeCount() {
    return 2; // Count of different layouts
}

if (convertView == null) { // You can move this line into your constructor, the inflater service won't change.

 mInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    if(getItemViewType(position) == 0)
        convertView = mInflater.inflate(R.layout.listview_item_product_complete, null);
    else
        convertView = mInflater.inflate(R.layout.listview_item_product_inprocess, null);
    // etc, etc...

If you want more information just go through the android recyler View document.

查看更多
手持菜刀,她持情操
4楼-- · 2019-06-07 05:46

maybe read this: https://stackoverflow.com/questions/6042781/android-listview

also you may use table layout with some minor changing.

查看更多
登录 后发表回答