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

recyclerView.setLayoutManager(gridLayoutManager);

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

if(gridLayoutManager.getItemCount() == 10){gridLayoutManager.setSpanCount(1);}else{gridLayoutManager.setSpanCount(2);
}

【问题讨论】:

  • 你可以在Recyclerview中取2个viewType,在设置之前,适配器列表根据位置定义视图类型

标签: java android android-studio gridview gridlayoutmanager


【解决方案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
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-19
      • 1970-01-01
      • 2017-03-21
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多