我将 gridLayout 与 spanCount 2 一起使用,但我想在每 10 个项目后显示 spanCount 1
I am using gridLayout with spanCount 2 but I want to show spanCount 1 after every 10 items
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(gridLayoutManager);
我正在尝试这个,但它不起作用,也没有发现任何错误我到处搜索,但找不到任何人可以提供帮助。
if(gridLayoutManager.getItemCount() == 10){gridLayoutManager.setSpanCount(1);}else{gridLayoutManager.setSpanCount(2);
}
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是重量,不是计数。
这是我的代码的一部分,每行设置 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) {
}
并且您在 fragment/activity
上设置了此代码
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
}
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(gridLayoutManager);
我正在尝试这个,但它不起作用,也没有发现任何错误我到处搜索,但找不到任何人可以提供帮助。
if(gridLayoutManager.getItemCount() == 10){gridLayoutManager.setSpanCount(1);}else{gridLayoutManager.setSpanCount(2);
}
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是重量,不是计数。
这是我的代码的一部分,每行设置 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) {
}
并且您在 fragment/activity
上设置了此代码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
}