recycleView (2) Grid, there is spacing in the middle, left, right, top, and bottom have no spacing

1. Function

1. Renderings

The top, bottom, right and left of item are all 0

2. Code

1. Key code

//Set the spacing between RecycleView items. The upper and lower spacing is 20 for sorting, and the left and right spacing is 20 for sorting.
        binding.rv.addItemDecoration(object : RecyclerView.ItemDecoration() {<!-- -->
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {<!-- -->
                val position = parent.getChildAdapterPosition(view) // Get the position of the item
                val spanCount = 2 // number of columns
                val itemCount = parent.adapter?.itemCount ?: 0 //Total number of items
                val spacing = 20 // spacing
                val row = position / spanCount // row number
                val lastRow = (itemCount - 1) / spanCount //The line number of the last row
                //If it is the first column, set right, if it is the second column, just set left
                if (position%spanCount==0){<!-- -->
                    outRect.right=20
                }else{<!-- -->
                    outRect.left=20
                }
                if (row == 0) {<!-- --> // If it is the first row, no upper spacing is set
                    outRect.top = 0
                } else {<!-- --> // Otherwise, set the upper spacing to 20
                    outRect.top = spacing
                }
                if (row == lastRow) {<!-- --> // If it is the last row, no lower spacing is set
                    outRect.bottom = 0
                } else {<!-- --> // Otherwise, set the lower spacing to 20
                    outRect.bottom = spacing
                }
            }
        })

2. Approximate code

//Create a list to store data
        val itemList = listOf(
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp),
            Item("Apple", R.drawable.ic_home_black_24dp)
        )

        //Set the layout manager of RecycleView to GridLayoutManager and specify the number of columns to 2
        binding.rv.layoutManager = GridLayoutManager(activity, 2)

        //Set the adapter of RecycleView to ItemAdapter and pass in the list data
        binding.rv.adapter = ItemAdapter(itemList)

        //Set the spacing between RecycleView items, the top and bottom spacing is 20 for sorting, and the left and right spacing is 20 for sorting
        binding.rv.addItemDecoration(object : RecyclerView.ItemDecoration() {<!-- -->
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {<!-- -->
                val position = parent.getChildAdapterPosition(view) // Get the position of the item
                val spanCount = 2 // number of columns
                val itemCount = parent.adapter?.itemCount ?: 0 //Total number of items
                val spacing = 20 // spacing
                val row = position / spanCount // row number
                val lastRow = (itemCount - 1) / spanCount //The line number of the last row
                //If it is the first column, set right, if it is the second column, just set left
                if (position%spanCount==0){<!-- -->
                    outRect.right=20
                }else{<!-- -->
                    outRect.left=20
                }
                if (row == 0) {<!-- --> // If it is the first row, no upper spacing is set
                    outRect.top = 0
                } else {<!-- --> // Otherwise, set the upper spacing to 20
                    outRect.top = spacing
                }
                if (row == lastRow) {<!-- --> // If it is the last row, no lower spacing is set
                    outRect.bottom = 0
                } else {<!-- --> // Otherwise, set the lower spacing to 20
                    outRect.bottom = spacing
                }
            }
        })

adapter and item

//Create a data class to store the data of the list
data class Item(val name: String, val image: Int)

//Create an adapter class, inherited from RecyclerView.Adapter
class ItemAdapter(private val itemList: List<Item>) : RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {<!-- -->

    //Create an inner class for binding views and data
    class ItemViewHolder(val binding: ItemLayoutBinding) : RecyclerView.ViewHolder(binding.root)

    // Override the onCreateViewHolder method to create views
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {<!-- -->
        val binding = ItemLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemViewHolder(binding)
    }

    // Override the onBindViewHolder method for binding data
    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {<!-- -->
        val item = itemList[position]
        holder.binding.itemName.text = item.name
        holder.binding.itemImage.setImageResource(item.image)
    }

    // Override the getItemCount method to return the size of the list
    override fun getItemCount(): Int {<!-- -->
        return itemList.size
    }
}

3. Summary