简体   繁体   English

使用colSpan和rowSpan的GridView [Android]

[英]GridView with colSpan and rowSpan [Android]

My question is about making a GridView with rowSpan and colSpan support. 我的问题是关于使用rowSpan和colSpan支持创建GridView。 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. 一种方法是实现具有不同行类型的自定义ListView适配器。 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. 使用getItemViewType(int position)可以帮助优化这样的场景,但我担心会有太多的ViewTypes会使回收效率低下。 Since I want to have a 3 cols grid and some elements having rowSpans as high as 4 or 5. 因为我想要一个3 cols网格和一些rowSpans高达4或5的元素。

Another way could extending ListView and make those "card"s have their own recycler. 另一种方法可以扩展ListView并使这些“卡”拥有自己的回收器。 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. 通常它对于ListView来说只是一行,但它应该有自己的子级和自己的层次结构。 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. 如果我们使用自定义回收器,所有卡(如上面的ascii艺术的“#”,“=”或“+”)应该转到特定于其尺寸的回收箱(例如4x2用于'#',1x4用于'=')并且回收的机会会更高。

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. 也许不使用ListView并在Scroller的帮助下实现一个全新的视图,使用自己的回收器和服务器端确定的每个元素的绝对位置,可能是一个更好的主意。

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 Java代码

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 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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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