java 具有行跨度和列跨度的自定义网格视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14441394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:16:31  来源:igfitidea点击:

Custom Grid View with Row span and Column span

javaandroidgridviewscrollhtml-table

提问by Karan_Rana

i am trying to implement a grid view which has the Graphical view as follows. I have gone through various blogs and S.O questions, and unfortunately i am not able to provide a row and column span to one particular grid item and grid view also does not support this feature. And i don't want to create Dynamic Scroll View with other views in between as the there is a lot of data and it causes performance issues. So if any one have any suggestions. Please reply . Thanks in advance. enter image description here

我正在尝试实现一个具有如下图形视图的网格视图。我浏览了各种博客和 SO 问题,不幸的是我无法为一个特定的网格项目提供行和列跨度,并且网格视图也不支持此功能。而且我不想创建带有其他视图的动态滚动视图,因为有很多数据并且会导致性能问题。所以如果有人有任何建议。请回复 。提前致谢。 在此处输入图片说明

采纳答案by Supreethks

Do you intend to repeat the pattern for your scrollable view? To be more clear, does your Grid item with large span repeat at regular intervals?

您是否打算为可滚动视图重复该模式?更清楚的是,您的跨度大的 Grid 项目是否定期重复?

One option would be to use a list view and implement your large span view with two normal views as one row of the list view with a tag such as "special_row" and implement regular views as another row with tag like "normal row". Based on the requirement you can recycle the rows by accessing the row tags.

一种选择是使用列表视图并使用两个普通视图将大跨度视图实现为列表视图的一行,并带有诸如“special_row”之类的标签,并将常规视图实现为带有诸如“普通行”之类的标签的另一行。根据需求,您可以通过访问行标记来回收行。

EDIT:

编辑:

I found a library that implements pinterest like UI for android. This has a symmetric views in it. Checkout PinterestLisView.

我找到了一个为 android 实现类似 UI 的 pinterest 库。这里面有一个对称的观点。结帐PinterestLisView

EDIT:

编辑:

Hereis another interesting technique by specifying coulmn span and row spans for the grid items. I took from this question. I guess you can do away with the static xml declaration of grid items by programmatically specifying column and row spans.

是另一种有趣的技术,通过为网格项目指定 coulmn span 和 row spans。我从这个问题。我想您可以通过以编程方式指定列和行跨度来取消网格项的静态 xml 声明。

回答by Felipe Lima

Here's the solution to all your problems: https://github.com/felipecsl/AsymmetricGridViewYeah, I got tired of Android not having a class like this and wrote it myself. Hope it is useful for you.

这是您所有问题的解决方案:https: //github.com/felipecsl/AsymmetricGridView是的,我厌倦了 Android 没有这样的课程,并自己编写了它。希望对你有用。

回答by Wei

This is a particular solution for 3 column grids with featured items that span 2x2 grids.

这是 3 列网格的特殊解决方案,其中的特色项目跨越 2x2 网格。

public class GridAdapter extends ArrayAdapter<GridAdapter.GridItem> {
    public GridAdapter(Context context, int itemViewResId, List<String> things) {
        super(context, itemViewResId, buildGridItems(things));
    }

    /**
     * Assumes 3 column layout. A list of indices that shows a large
     * item on the right of 1st row, then alternating on every 3rd row 
     * to the left and then right. The large item should occupy a 2x2 grid.
     *
     *   X O O
     *   X O O
     *   X X X
     *   X X X
     *   O O X
     *   O O X
     *   X X X
     *   X X X
     *   X O O
     *   X O O
     *   X X X
     *
     * The indices where the large featured items are in item list is 
     * 1, 9, 19, 27, 37, 45, 55, ...         
     */
    protected static List<Integer> getFeaturedIndex(int total) {
        int pos = 1;
        ArrayList<Integer> index = new ArrayList<Integer>();
        if (pos + 1 < total) {
            index.add(pos);
        }
        for (int i = 0; pos < total; i++) {
            int inc = i % 2 == 0 ? 8 : 10;
            pos += inc;
            if (pos + 1 < total) {
                index.add(pos);
            }
        }
        return index;
    }

    protected static List<GridItem> buildGridItems(List<String> things) {
        ArrayList<GridItem> items = new ArrayList<GridItem>();
        List<Integer> featuredIndex = getFeaturedIndex(things.size());
        ArrayList<GridItem> featured = new ArrayList<GridItem>();
        for (int i = 0, k = things.size(); i < k; i++) {
            GridItem item = new GridItem(things.get(i));
            if (featuredIndex.contains(i)) {
                item.feature = true;
                featured.add(item);
            }
            items.add(item);
        }
        for (GridItem feature : featured) {
            int index = items.indexOf(feature);
            GridItem shim = new GridItem(feature.getModel());
            shim.shim = true;
            items.add(index + 1, shim);
            items.add(index + 3, shim);
            items.add(index + 4, shim);
        }
        return items;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).shim ? 0 : 1;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new View(getContext());
        }
        GridItem item = getItem(position);
        if (item.feature) {
            convertView.setLayoutParams(new LayoutParams(400,300));
        } else {
            convertView.setLayoutParams(new LayoutParams(200,150));
        }
        if (item.shim) {
            convertView.setVisibility(View.GONE);
        }
        return convertView;
    }

    public static class GridItem {
        private String mItem;
        private boolean shim = false;
        private boolean feature = false;

        public GridItem(String item) {
            mItem = item;
        }
    }
}

The idea is to wrap the item list with GridItem with a featureand shimflags that determine how the views should behave.

我们的想法是用包裹在的GridItem项目列表与featureshim标志决定的看法应该如何表现。

The method getFeaturedIndex()calculates which items in the original list should be featured. Then in the buildGridItems()we take 2 steps. First, flag all the items that are featured (and keep a list of these items). Afterwards, for each of these featured items, add 3 shims (+1, +3 and +4) relative to the featured item.

该方法getFeaturedIndex()计算原始列表中的哪些项目应该有特色。然后在buildGridItems()我们采取2个步骤。首先,标记所有有特色的项目(并保留这些项目的列表)。之后,对于这些特色项目中的每一个,相对于特色项目添加 3 个垫片(+1、+3 和 +4)。

In the getView()for featured items, we set the appropriate dimensions as 2x2 of the normal items. For shim items, set the visibility to GONE.

getView()特色商品中,我们将适当的尺寸设置为普通商品的 2x2。对于 shim 项目,将可见性设置为GONE