简体   繁体   English

GridView全角分隔符

[英]Gridview full width separators

I have ArrayList of some data. 我有一些数据的ArrayList ArrayList includes different category of objects (cat 1, cat 2, cat 3, ...). ArrayList包括不同类别的对象(类别1,类别2,类别3,...)。 I want to display my list in GridView . 我想在GridView显示我的列表。

My xml looks: 我的xml看起来:

 <GridView
                android:id="@+id/gv_my_grid"
                style="@style/fill_wrap"
                android:cacheColorHint="@null"
                android:layout_margin="10dp"
                android:numColumns="3"
                android:listSelector="@android:color/transparent"
                android:stretchMode="columnWidth"
                android:gravity="center"
                />

And all works great. 并且所有作品都很棒。

GridView looks like: GridView看起来像:

|item 1 (cat 1)| |item 2 (cat 1)| |item 3 (cat 1)|
|item 4 (cat 1)| |item 5 (cat 2)| |item 6 (cat 2)|
|item 7 (cat 3)| |item 8 (cat 3)| |item 9 (cat 3)|

But now I want to place full width horisontal separator line between different category items. 但是现在我想在不同类别的项目之间放置水平宽度分隔线。 It must look like: 它必须看起来像:

___________category_1_____________________________
|item 1 (cat 1)| |item 2 (cat 1)| |item 3 (cat 1)|
|item 4 (cat 1)| 
___________category_2_____________________________
|item 5 (cat 2)| |item 6 (cat 2)|
___________category_1_____________________________
|item 7 (cat 3)| |item 8 (cat 3)| |item 9 (cat 3)|

Is this possible? 这可能吗? Cause all what I found - only ListView implementation. 导致所有我发现的-仅ListView实现。

Edit: 编辑:

I was asked to add the code for my adapter. 我被要求为我的适配器添加代码。 I think getView method will be enough. 我认为getView方法就足够了。

public View getView(int position, View view, ViewGroup viewGroup) {
        final Holder holder;
        final View cell;
        if (null == view) {
            cell = this.inflater.inflate(R.layout.gi_my_item, null);
            holder = new Holder(cell);
            cell.setTag(holder);
        } else {
            cell = view;
            holder = (Holder) cell.getTag();
        }
        holder.item = this.items.get(position);
        holder.textField.setText(holder.item.someText);
        return cell;
    }

I found that you can use RecyclerView with GridLayoutManager to do the stuff. 我发现可以将RecyclerViewGridLayoutManager一起使用来做这些事情。

Here is the code how to do this. 这是代码如何执行此操作。

GridLayoutManager manager = new GridLayoutManager(this, 3);

that creates the grid with three columns. 创建具有三列的网格。

Use setSpanSizeLookup() method to have a callback to return the column span of each item. 使用setSpanSizeLookup()方法进行回调以返回每个项目的列范围。

manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (elementsAdapter.getItemViewType(position) == VIEW_HEADER) ? 3 : 1;
            }
        });

that will made three column span when it was the header type. 当它是标题类型时,将跨三列。

Also, you have to make two of RecyclerView.ViewHolder and use will need getItemViewType() to differ from item views and header views in your RecyclerView.Adapter . 另外,您必须制作两个RecyclerView.ViewHolder并使用getItemViewType()来与RecyclerView.Adapter项目视图和标头视图不同。

I made some sample code for it. 我为此做了一些示例代码。
https://github.com/j796160836/GridLayoutManagerDemo https://github.com/j796160836/GridLayoutManagerDemo


Reference: 参考:
https://medium.com/android-bites/grid-layout-with-header-3f2966181c4d https://medium.com/android-bites/grid-layout-with-header-3f2966181c4d

To reach this purpose,,You have to composite GridView in a ListView . 为了达到这个目的,您必须在ListView组合GridView with a CustomAdapter you can do that, ListView as parent and GridView as row_item . 使用CustomAdapter可以做到这一点, ListView作为父级, GridView作为row_item

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

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