Sobes.tech
Middle

ItemDecoration nədir və Android-də necə istifadə olunur?

sobes.tech Süni İntellekt

AI-dan cavab

ItemDecoration RecyclerView-in fərdi elementlərinin və ya bütün RecyclerView lövhəsinin ətrafında dekorasiyaların çəkilməsi üçün istifadə olunur.

Əsas funksiyalar:

  • Elementlər arasında ayırıcılar (xətlər, boşluqlar) çəkmək.
  • Elementlər üçün fonlar və ya çərçivələr çəkmək.
  • Elementlərin ətrafına (padding) boşluqlar əlavə etmək, elementin layout parametrlərini dəyişmədən.

İstifadə qaydası:

  1. RecyclerView.ItemDecoration-u miras alan bir sinif yaradın.
  2. Lazımi metodları yenidən təyin edin:
    • onDraw(Canvas c, RecyclerView parent, RecyclerView.State state): elementlərin altına dekorasiyalar çəkmək üçün.
    • onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state): elementlərin üstünə dekorasiyalar çəkmək üçün.
    • getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state): elementin sərhədlərinə boşluqlar əlavə etmək üçün.
  3. ItemDecoration sinifinin nümunəsini yaradın.
  4. Onu RecyclerView-a addItemDecoration(ItemDecoration decoration) vasitəsilə əlavə edin.

getItemOffsets metodunun boşluq əlavə etmək üçün nümunəsi:

class SpaceItemDecoration(private val spaceHeight: Int) : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        with(outRect) {
            if (parent.getChildAdapterPosition(view) == 0) {
                top = spaceHeight // Yuxarı boşluq yalnız ilk element üçün
            }
            left =  spaceHeight
            right = spaceHeight
            bottom = spaceHeight
        }
    }
}

// RecyclerView-in tənzimlənməsi zamanı kod:
// val recyclerView = findViewById<RecyclerView>(R.id.my_recycler_view)
// recyclerView.addItemDecoration(SpaceItemDecoration(resources.getDimensionPixelSize(R.dimen.item_space)))

onDraw metodunun ayırıcı çəkmək üçün nümunəsi:

class DividerItemDecoration(private val dividerHeight: Int, private val dividerColor: Int) : RecyclerView.ItemDecoration() {
    private val paint = Paint().apply {
        color = dividerColor
        style = Paint.Style.FILL
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight

        for (i in 0 until parent.childCount - 1) { // İkinci ən son elementə qədər iterasiya
            val child = parent.getChildAt(i)
            val top = child.bottom
            val bottom = top + dividerHeight
            canvas.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
        }
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
         // Son element xaricində ayırıcı üçün yer əlavə edin
        if (parent.getChildAdapterPosition(view) != parent.adapter?.itemCount?.minus(1)) {
             outRect.bottom = dividerHeight
        }
    }
}

// RecyclerView-in tənzimlənməsi zamanı kod:
// val recyclerView = findViewById<RecyclerView>(R.id.my_recycler_view)
// recyclerView.addItemDecoration(DividerItemDecoration(resources.getDimensionPixelSize(R.dimen.divider_height), ContextCompat.getColor(this, R.color.divider_color)))

Əsasən, ItemDecoration elementlərin ölçüsünü və yerləşməsini dəyişmir, yalnız onların çəkilməsi və ətrafındakı boşluqlara təsir edir.