简体   繁体   中英

GridView with colSpan and rowSpan [Android]

My question is about making a GridView with rowSpan and colSpan support. I looked for it on stack overflow etc. there are a few similar questions but no valid answers.

One way of doing that could be implementing a custom ListView Adapter with different row types. Using getItemViewType(int position) could help optimising for such a scenario but I'm afraid that there will be so much ViewTypes that will make recycling inefficient. Since I want to have a 3 cols grid and some elements having rowSpans as high as 4 or 5.

Another way could extending ListView and make those "card"s have their own recycler. I mean, If there is a row like this:

    ##..//
    ##**//
    ##====
    ##++--  

normally it would be a single row for ListView but it should have its own childs and own hierarchy. This row would never be reused since it's really unique. If we use a custom recycler, all the cards (like '#', '=' or '+' for the ascii art above) should go to recycle bins specific to their sizes (like 4x2 for the '#', 1x4 for the '=' ) and the chance of recycling will be much higher.

Maybe not using the ListView and implementing a whole new view with help of Scroller, using its own recycler and absolute positions for each element determined on server side, could be a better idea.

I am asking you this, because I think you may come up with a better idea or at least give me some advice on how hard could be implementing each way.

Java Code

private LinearLayout addNewSpannedView(Integer resourceId, ViewGroup rootElement) {
    return (LinearLayout) ((ViewGroup) getLayoutInflater().inflate(resourceId, rootElement, true)).getChildAt(rootElement.getChildCount() - 1);
}
// set columnSpan depending on some logic (gridLayout is the layout to add the view's to -> in my case these are LinearLayouts)
shape = addNewSpannedView(columnSpan == 1 ? R.layout.grid_ll_col_span_1 : R.layout.grid_ll_col_span_2, gridLayout);

grid_ll_col_span_2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/shapeWidth"
    android:layout_height="wrap_content"
    android:layout_columnSpan="2"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM