recycleView(二)Grid,中间有间距,left,right,top,bottom没有间距

1.作用

1.效果图

item的top,bottom,right,left都是0
recycleView(二)Grid,中间有间距,left,right,top,bottom没有间距_第1张图片

2.代码

1.关键代码

// 设置RecycleView的item间的间距,上下间距为20排序,左右间距为20排序
        binding.rv.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                val position = parent.getChildAdapterPosition(view) // 获取item的位置
                val spanCount = 2 // 列数
                val itemCount = parent.adapter?.itemCount ?: 0 // item总数
                val spacing = 20 // 间距
                val row = position / spanCount // 行号
                val lastRow = (itemCount - 1) / spanCount // 最后一行的行号
                //如果是第一列就设置right,如果是第二列就设置left就好了
                if (position%spanCount==0){
                    outRect.right=20
                }else{
                    outRect.left=20
                }
                if (row == 0) { // 如果是第一行,不设置上间距
                    outRect.top = 0
                } else { // 否则,设置上间距为20
                    outRect.top = spacing
                }
                if (row == lastRow) { // 如果是最后一行,不设置下间距
                    outRect.bottom = 0
                } else { // 否则,设置下间距为20
                    outRect.bottom = spacing
                }
            }
        })

2.大概代码

// 创建一个列表,用于存储数据
        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)
        )

        // 设置RecycleView的布局管理器为GridLayoutManager,指定列数为2
        binding.rv.layoutManager = GridLayoutManager(activity, 2)

        // 设置RecycleView的适配器为ItemAdapter,传入列表数据
        binding.rv.adapter = ItemAdapter(itemList)

        // 设置RecycleView的item间的间距,上下间距为20排序,左右间距为20排序
        binding.rv.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                val position = parent.getChildAdapterPosition(view) // 获取item的位置
                val spanCount = 2 // 列数
                val itemCount = parent.adapter?.itemCount ?: 0 // item总数
                val spacing = 20 // 间距
                val row = position / spanCount // 行号
                val lastRow = (itemCount - 1) / spanCount // 最后一行的行号
                //如果是第一列就设置right,如果是第二列就设置left就好了
                if (position%spanCount==0){
                    outRect.right=20
                }else{
                    outRect.left=20
                }
                if (row == 0) { // 如果是第一行,不设置上间距
                    outRect.top = 0
                } else { // 否则,设置上间距为20
                    outRect.top = spacing
                }
                if (row == lastRow) { // 如果是最后一行,不设置下间距
                    outRect.bottom = 0
                } else { // 否则,设置下间距为20
                    outRect.bottom = spacing
                }
            }
        })

adapter与item

// 创建一个数据类,用于存储列表的数据
data class Item(val name: String, val image: Int)

// 创建一个适配器类,继承自RecyclerView.Adapter
class ItemAdapter(private val itemList: List<Item>) : RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {

    // 创建一个内部类,用于绑定视图和数据
    class ItemViewHolder(val binding: ItemLayoutBinding) : RecyclerView.ViewHolder(binding.root)

    // 重写onCreateViewHolder方法,用于创建视图
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val binding = ItemLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemViewHolder(binding)
    }

    // 重写onBindViewHolder方法,用于绑定数据
    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val item = itemList[position]
        holder.binding.itemName.text = item.name
        holder.binding.itemImage.setImageResource(item.image)
    }

    // 重写getItemCount方法,用于返回列表的大小
    override fun getItemCount(): Int {
        return itemList.size
    }
}

3.总结

你可能感兴趣的:(android)