Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Search Gists
Sign in Sign up

Instantly share code, notes, and snippets.

@flyfire
Forked fromshawnlinboy/BlankItemDecoration.kt
CreatedDecember 23, 2023 03:13
    • Star(0)You must be signed in to star a gist
    • Fork(0)You must be signed in to fork a gist
    Save flyfire/3b5e9d010b909bf77276318be70817ab to your computer and use it in GitHub Desktop.
    An ItemDecoration implementation which provides functionality that add extra blank space between list items.
    /*
    * Copyright (C) 2019 Shen Lin
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
    importandroid.graphics.Rect
    importandroid.view.View
    importandroidx.recyclerview.widget.RecyclerView
    /**
    * An [RecyclerView.ItemDecoration] implementation which provides
    * functionality that add extra blank space between list items.
    *
    * @param spaceHeight The empty space height between two items.
    * @param orientation Layout orientation. Should be [BlankItemDecoration.HORIZONTAL] or [BlankItemDecoration.VERTICAL]
    * @param firstItemPaddingStart Pass non-zero value if first item need a paddingStart
    * @param lastItemPaddingEnd Pass non-zero value if last item need a paddingEnd
    */
    classBlankItemDecoration(
    privatevalorientation:Int =VERTICAL,
    privatevalspaceHeight:Int =0,
    privatevalfirstItemPaddingStart:Int =0,
    privatevallastItemPaddingEnd:Int =0,
    ) : RecyclerView.ItemDecoration() {
    companionobject {
    constvalHORIZONTAL=0
    constvalVERTICAL=1
    }
    init {
    if (orientation!=HORIZONTAL&& orientation!=VERTICAL) {
    throwIllegalArgumentException("invalid orientation:$orientation")
    }
    }
    overridefungetItemOffsets(
    outRect:Rect,view:View,parent:RecyclerView,
    state:RecyclerView.State,
    ) {
    if (parent.adapter!=null) {
    val totalItemCount= parent.adapter?.itemCount?:0
    val itemPosition= parent.getChildAdapterPosition(view)
    if (itemPosition!= totalItemCount-1) {
    //For all the items except the last one
    with(spaceHeight) {
    when (orientation) {
    HORIZONTAL-> outRect.right=this
    VERTICAL-> outRect.bottom=this
    }
    }
    //This is the first item
    if (itemPosition==0) {
    drawFirstItemPadding(outRect)
    }
    }else {
    //This is the last, or the first item if total item count is 1
    if (totalItemCount==1) {
    drawFirstItemPadding(outRect)
    }
    drawLastPaddingEnd(outRect)
    }
    }else {
    super.getItemOffsets(outRect, view, parent, state)
    }
    }
    privatefundrawLastPaddingEnd(outRect:Rect?) {
    if (lastItemPaddingEnd!=0) {
    with(lastItemPaddingEnd) {
    when (orientation) {
    HORIZONTAL-> outRect?.right=this
    VERTICAL-> outRect?.bottom=this
    }
    }
    }
    }
    privatefundrawFirstItemPadding(outRect:Rect?) {
    if (firstItemPaddingStart!=0) {
    with(firstItemPaddingStart) {
    when (orientation) {
    HORIZONTAL-> outRect?.left=this
    VERTICAL-> outRect?.top=this
    }
    }
    }
    }
    }
    Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

    [8]ページ先頭

    ©2009-2025 Movatter.jp