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

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

问题描述

我想有一个gridview类似这样

I want to have a gridview similar to this

每个奇数行将有大尺寸的两个图像和偶数行将会有四个小images.How我能做到这一点?

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 。 RecyclerView XML的:

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并设置适配器 - 从正常适配器类继承的 RecyclerView.Adapter&LT;您的视图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&安培; H查看。您将需要创建2 XML视图TYPE_HIGH和其他视图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天全站免登陆