如何在android gridview中为行设置不同的列 [英] How to set different columns for rows in android gridview

查看:23
本文介绍了如何在android gridview中为行设置不同的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个类似这样的网格视图

I want to have a gridview similar to this

每个奇数行将有两个大尺寸的图像,偶数行将有四个较小的图像.我怎样才能做到这一点?

Every odd numbered row will have two images of big size and even numbered rows will have four smaller images.How can I achieve this?

推荐答案

我有类似的问题,我用新的 RecyclerView 解决了.

I have something similar and i solved with the new RecyclerView.

我创建了一个带有 RecyclerView 的 Fragment.xml 上的 RecyclerView:

I created a Fragment with an a RecyclerView. RecyclerView on xml:

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/filter_subtypes" android:layout_width="match_parent" android:layout_height="match_parent" />

关于您的片段/活动(在我的片段案例中为 OnViewCreated).我找到了 RecyclerView 并设置了一个 Adapter- 一个普通的 Adapter 类继承自 RecyclerView.Adapter<您的 VIEW HOLDER 类 >-然后我创建了一个 GridLayoutManager

On your Fragment/Activity (OnViewCreated in my fragment case). I find the RecyclerView and set an Adapter- a normal Adapter class inherit from RecyclerView.Adapter< YOUR VIEW HOLDER class >- And then i create a GridLayoutManager

final GridLayoutManager mng_layout = new GridLayoutManager(this.getActivity(), TOTAL_CELLS_PER_ROW/*In your case 4*/);


然后我覆盖这个方法来设置一个动态的列数(单元格)


Then i override this method to set a dynamic numbers of columns (cells)

mng_layout.setSpanSizeLookup( new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch( adapterSubtype.getItemViewType(position) ) {
                    case FilterSubtypesAdapter.TYPE_LOW:
                        return TOTAL_CELLS_PER_ROW;
                    case FilterSubtypesAdapter.TYPE_HIGH:
                        return 2;
                    default:
                        return -1;
                }
            }
        });
myRecyclerView.setLayoutManager(mng_layout);

有了这个,您将在行中获得动态数量的单元格.

With this you will get dynamic numbers of cell on your rows.

额外:然后,如果您在适配器上使用相同的视图/类型视图,您将获得相同的 w &视图.您需要为 TYPE_HIGH 创建 2 个 xml 视图,为 TYPE_LOW 创建其他视图.

EXTRA: Then if you are using the same view/type view on your adapter, you will get the same w & h view. You will need to create 2 xml views for TYPE_HIGH and other view for TYPE_LOW.

因此,在您的适配器中,您需要有 2 种数据(1 种用于高图像,1 种用于低图像).您必须覆盖此方法

So, in your adapter, you need to have 2 kind of data (1 for high images and 1 for low images). You must override this methods

@Override
public SubtypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = null;
    if (viewType==TYPE_HIGH) {
        view = inflater.inflate(R.layout.item_image_high, parent, false);
    } else {
        view = inflater.inflate(R.layout.item_image_low, parent, false);
    }
    return new SubtypeViewHolder(view, viewType);
}

 @Override
 public int getItemViewType(int position) {
     return (list.get(position).getType()==Subtype_type.HIGH) ? TYPE_HIGH : TYPE_LOW;
 }

我希望我说的很清楚,有任何问题告诉我.

I hope i was clear, any problem tell me.

这篇关于如何在android gridview中为行设置不同的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆