如何有一个头GridLayoutManager,而不改变其适配器? [英] How to have a GridLayoutManager with header, without changing its adapter?

查看:834
本文介绍了如何有一个头GridLayoutManager,而不改变其适配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我有一个应用程序,示出了网格状的方式的项目的列表,但不完全...

问题


  1. 有些物品具有不同的高度,所以在他们附近的那些应获得相同的高度,因为他们有。这一件作品上GridLayoutManager。


  2. 某些项目(实际上只有第一个在我的情况),需要跨越整行(这就是为什么我用StaggeredGridLayoutManager)。


使用正常GridLayoutManager,首先要求微细加工:每一行可以有不同的高度。但由于#2,它实际上毁了#1。

问题

时有可能使用StaggeredGridLayoutManager这样,当项目具有不同的高度,不会让他们在Y坐标移动?

我在想:也许我可以用NestedScrollView和GridLayoutManager(因为只有第一项跨越),不过,我想知道是否有可能在其余情形下(而且这个解决方案)

解决方案

好了,我已经找到了一个可能的解决方案仍用GridLayoutManager:


  • 使用<一个href=\"http://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemViewType(int)\"相对=nofollow> getItemViewType 并返回跨区项目特定的值,和正常的项目不同的值。


  • 创建<一个需要的视图href=\"http://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#onCreateViewHolder(android.view.ViewGroup,%20int)\"相对=nofollow> onCreateViewHolder ,根据其类型,并投以必要的viewHolder类<一href=\"http://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#onBindViewHolder(VH,%20int)\"相对=nofollow> onBindViewHolder 根据类型。


  • 使用<一个href=\"http://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.html#setSpanSizeLookup(android.support.v7.widget.GridLayoutManager.SpanSizeLookup)\"相对=nofollow> setSpanSizeLookup 了,里面getSpanSize,返回跨区细胞的情况下,类型是此类项目


例如:

  @覆盖
  公众诠释getItemViewType(最终诠释位置)
    {
    回到位置== 0 VIEW_TYPE_HEADER:VIEW_TYPE_NORMAL;
    }  公共ViewHolder onCreateViewHolder(最终母公司的ViewGroup,最终诠释viewType)
    {
    如果(viewType == VIEW_TYPE_HEADER)... //创建头ViewHolder
    否则... //创建普通项目
    }
...
  _layoutManager =新GridLayoutManager(...,3,LinearLayoutManager.VERTICAL,FALSE);
  _layoutManager.setSpanSizeLookup(新SpanSizeLookup()
      {
      @覆盖
      公众诠释getSpanSize(最终诠释位置)
        {
        返回_adapter.getItemViewType(位置)== VIEW_TYPE_HEADER _layoutManager.getSpanCount():1;
        }
      });

不过,我想知道如何使用NestedScrollView解决方案

这是我注意到的一个问题是,如果我尝试设置头GONE的知名度,它仍然需要空间。

Background

I have an app that shows a list of items in a grid-like way, yet not exactly...

The problem

  1. Some of the items have a different height, so the ones near them should gain the same height as they have. This one works on GridLayoutManager.

  2. Some items (actually only the first one in my case) need to span the entire row (which is why I used StaggeredGridLayoutManager ).

Using the normal GridLayoutManager, the first requirement worked fine: each row could have a different height. But because of #2, it actually ruined #1.

The question

Is it possible to use StaggeredGridLayoutManager so that when items have different height, it won't make them move in the Y coordinate?

I was thinking: maybe I could use NestedScrollView and GridLayoutManager (because only the first item is spanned), but still, I would like to know if it's possible in the rest of the cases (and also this solution).

解决方案

OK, so I've found a possible solution by still using GridLayoutManager:

  • use getItemViewType and return a specific value for spanned items, and a different value for normal items .

  • Create the needed view in onCreateViewHolder, according to its type, and cast to the needed viewHolder class in onBindViewHolder according to the type.

  • use setSpanSizeLookup, and inside getSpanSize, return the spanned cells in case the type is for such items

Example:

  @Override
  public int getItemViewType(final int position)
    {
    return position==0?VIEW_TYPE_HEADER:VIEW_TYPE_NORMAL;
    }

  public ViewHolder onCreateViewHolder(final ViewGroup parent,final int viewType)
    {
    if(viewType==VIEW_TYPE_HEADER) ... //create header ViewHolder
    else ... // create normal item
    }


...
  _layoutManager=new GridLayoutManager(...,3,LinearLayoutManager.VERTICAL,false);
  _layoutManager.setSpanSizeLookup(new SpanSizeLookup()
      {
      @Override
      public int getSpanSize(final int position)
        {
        return _adapter.getItemViewType(position)==VIEW_TYPE_HEADER?_layoutManager.getSpanCount():1;
        }
      });

Still, I would like to know how to use the NestedScrollView solution

One issue that I have noticed, is that if I try to set the visibility of the header to GONE, it still takes space.

这篇关于如何有一个头GridLayoutManager,而不改变其适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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