我正在使用带有 spanCount 2 的 gridLayout 但我想在每 10 个项目后显示 spanCount 1

Posted

技术标签:

【中文标题】我正在使用带有 spanCount 2 的 gridLayout 但我想在每 10 个项目后显示 spanCount 1【英文标题】:I am using gridLayout with spanCount 2 but I want to show spanCount 1 after every 10 items 【发布时间】:2021-03-05 22:48:15 【问题描述】:
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);

recyclerView.setLayoutManager(gridLayoutManager);

我正在尝试这个,但这不起作用,没有任何错误发现我到处搜索,但没有任何人可以帮助。

if(gridLayoutManager.getItemCount() == 10)gridLayoutManager.setSpanCount(1);elsegridLayoutManager.setSpanCount(2);

【问题讨论】:

你可以在Recyclerview中取2个viewType,在设置之前,适配器列表根据位置定义视图类型 【参考方案1】:
val gridLayoutManager = GridLayoutManager(this, 2)
gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() 
    override fun getSpanSize(position: Int): Int 
        return if (position % 10 == 0) 2 else 1
    

rv.layoutManager = gridLayoutManager

我用的是kotlin,你可以用Java重写它,只需使用Java中的gridLayoutManager.setSpanSizeLookup()方法即可。注意 spanSize 表示重量,而不是计数。

【讨论】:

【参考方案2】:

这是我的代码的一部分,每行设置 3 个项目,每 9 个项目后设置 spanCount 1 您可以将此 kotlin 代码转换为 java 并设置您的约束

private val SHOP_TYPE = 1
private val AD_TYPE = 2

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder 
    if(viewType == SHOP_TYPE) 
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_shop_home, parent, false)
        return ShopViewHolder(view)
    
    else 
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_ad_search, parent, false)
        return AdsViewHolder(view)
    


override fun getItemCount(): Int 
    return shopsList.size + adsList.size


override fun onBindViewHolder(holder: ViewHolder, position: Int) 
    val countAds = (position + 1) / 10
    val indexAd = if(((position + 1) / 10) - 1 < 0) 0 else ((position + 1) / 10) - 1
    val isLastOne = (position + 1) == (shopsList.size + adsList.size)
    if((position + 1) % 10 == 0 || isLastOne) 
        val adsHolder = holder as AdsViewHolder
        adsHolder.bind(adsList[indexAd], adsClickListener)
    
    else 
        val shopHolder = holder as ShopViewHolder
        shopHolder.bind(shopsList[position - countAds], shopClickListener)
    


override fun getItemViewType(position: Int): Int 
    if((position + 1) % 10 == 0 || (position + 1) == (shopsList.size + adsList.size))
        return AD_TYPE
    return SHOP_TYPE


open class ViewHolder(view: View): RecyclerView.ViewHolder(view)


class ShopViewHolder(view: View): ViewHolder(view) 



class AdsViewHolder(private val view: View): ViewHolder(view) 
    

然后您在片段/活动中设置此代码

private fun setRecyclerView() 
    searchAdapter = SearchAdapter(shopsList, adsList)
    val gridLayoutManager = GridLayoutManager(requireContext(), 3)
    gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() 
        override fun getSpanSize(position: Int): Int 
            if((position + 1) % 10 == 0 || (position + 1) == (shopsList.size + adsList.size))
                return 3
            return 1
        
    
    rootView.rvSearch.layoutManager = gridLayoutManager
    rootView.rvSearch.setHasFixedSize(true)
    rootView.rvSearch.adapter = searchAdapter

【讨论】:

以上是关于我正在使用带有 spanCount 2 的 gridLayout 但我想在每 10 个项目后显示 spanCount 1的主要内容,如果未能解决你的问题,请参考以下文章

带有 GridLayoutManager 和 Picasso 的 RecyclerView 显示错误的图像

使用 GridLayoutManager 实现 RecyclerView 时将项目居中

使用 CSS 模块全局导入外部样式表

使用 jasmin 使用带有 async/await 的 $httpBackend 测试角度服务

Android TV RecyclerView重点互动

格雷码