【问题标题】:Setting span size of single row in StaggeredGridLayoutManager在 StaggeredGridLayoutManager 中设置单行的跨度大小
【发布时间】:2016-02-15 05:34:57
【问题描述】:

我有一个有 2 列的交错网格。这是有效的。我想要的是在位置 0 使行跨越 2 列。我之前很容易使用 GridLayoutManger 做到了这一点:

                mGridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        return position == 0 ? 2 : 1;
                    }
                });

StaggeredGridLayoutManager 不像 GridLayoutManager 那样为我提供此功能。

有没有其他方法可以做到这一点?我已经搜索但没有找到任何有相同问题的人,这令人惊讶,因为当 RecyclerView 的最后一行显示 ProgressBar 时,我认为此功能对我的场景和无限滚动非常有用。

【问题讨论】:

    标签: android android-recyclerview android-support-library staggeredgridlayout


    【解决方案1】:

    您可以使用setFullSpan 方法。
    这样,项目将使用所有跨度区域进行布局。

    这意味着,如果方向是垂直的,则视图将具有全宽;如果方向是水平的,则视图将具有全高。

    类似这样的:

    public final void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    
        StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams();
        layoutParams.setFullSpan(true);
    }
    

    注意。
    它支持跨越所有列的视图,但对于您的情况应该足够了。

    【讨论】:

    • 正是我想要的。非常感谢加布里埃尔!
    • 如果您希望特定项目恰好跨越 2 列(共 3 列)怎么办?而不是跨越所有列。
    • 一种优化,如果特定视图类型(例如标题)的所有视图都是完整的,您应该在onCreateViewHolder() 中设置。如果某些视图是完整的,而有些不是你必须在 onBind 中完成
    • 我用这个方法得到了java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.StaggeredGridLayoutManager$LayoutParams
    • @WitaloBenicio 您应该确保您在 RecyclerView 上设置的 LayoutManager 是(或正在扩展)StaggeredGridLayoutManager
    【解决方案2】:

    对于任何使用 Kotlin 的人。

    onBindViewHolder 中使用 isFullSpan 有效。

    override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) {
            holder.bind(loadState)
            val layoutParams = holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
            layoutParams.isFullSpan = true
        }
    

    【讨论】:

      【解决方案3】:

      正如@Daniele Segato 所说,

      由于 ViewHolder 不应该改变,我们必须保持 onBindViewHolder 的精益,最好在 onCreateViewHolder 方法中设置 isFullSpan 参数。这个LayoutParamter是给父View,也就是RecylcerView,来决定子view的布局方式。

      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StaggeredProductCardViewHolder {
          var isFullSpan = false
          var layoutId = R.layout.shr_staggered_product_card_first
          if (viewType == 1) {
              layoutId = R.layout.shr_staggered_product_card_second
          } else if (viewType == 2) {
              layoutId = R.layout.shr_staggered_product_card_third
              isFullSpan = true
      
          }
      
          val layoutView = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
          (layoutView.layoutParams as StaggeredGridLayoutManager.LayoutParams).isFullSpan= isFullSpan
          return StaggeredProductCardViewHolder(layoutView)
      }
      

      Final Result: Don't use gridLayoutManger's SpanCoutLookUp Method

      【讨论】:

        猜你喜欢
        • 2016-10-17
        • 2019-07-01
        • 2021-07-15
        • 1970-01-01
        • 2018-01-10
        • 1970-01-01
        • 2016-01-15
        • 1970-01-01
        • 2020-10-19
        相关资源
        最近更新 更多