程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在kotlin中以编程方式更改形状按钮半径并填充recyclerview项目?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在kotlin中以编程方式更改形状按钮半径并填充recyclerview项目??

开发过程中遇到如何在kotlin中以编程方式更改形状按钮半径并填充recyclerview项目?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在kotlin中以编程方式更改形状按钮半径并填充recyclerview项目?的解决方法建议,希望对你解决如何在kotlin中以编程方式更改形状按钮半径并填充recyclerview项目?有所启发或帮助;

我使用 itemtouchhelper SimpleCallback 位图和画布制作了滑动按钮。 我有两个问题。

  1. 如何使用位图和画布更改半径滑动按钮?
  2. 如何使用滑动按钮填充半径 recylcervIEw 项目?

iamdoing

这就是我正在做的,

我想做这样的, whatiwanttodo

我的代码是 MemoSwipeHelper.kt

package com.softsquared.template.kotlin.src.main.today

import androID.annotation.Suppresslint
import androID.content.Context
import androID.graphics.Canvas
import androID.graphics.Point
import androID.graphics.Rect
import androID.graphics.RectF
import androID.vIEw.GestureDetector
import androID.vIEw.MotionEvent
import androID.vIEw.VIEw
import androIDx.recyclervIEw.Widget.itemtouchhelper
import androIDx.recyclervIEw.Widget.RecyclerVIEw
import com.softsquared.template.kotlin.src.main.today.adapter.MemoAdapter
import java.util.*
import kotlin.collections.ArrayList

abstract class MemoSwipeHelper(adapter: MemoAdapter,context:Context,private val recyclerVIEw:RecyclerVIEw,internal var buttonWIDth:Int)
    :itemtouchhelper.SimpleCallback(itemtouchhelper.UP.or(itemtouchhelper.DOWN),itemtouchhelper.left) {
    val dragAdapter = adapter
    private var buttonList:MutableList<Swipebutton> ?= null
    lateinit var gestureDetector:GestureDetector
    var swipeposition = -1
    var swipeThreshold = 0.5f
    val buttonBuffer:MutableMap<Int,MutableList<Swipebutton>> = mutableMapOf()
    lateinit var removeQueue:linkedList<Int>

    abstract fun instantiateMybutton(vIEwHolder:RecyclerVIEw.VIEwHolder,buffer:MutableList<Swipebutton>)


    private val gestureListener = object:GestureDetector.SimpleOnGestureListener(){
        overrIDe fun onSingleTapUp(e: MotionEvent?): Boolean {
            for(button in buttonList!!){
                if(button.onClick(e!!.x,e!!.y))
                    break
            }
            return true
        }
    }

    @Suppresslint("ClickableVIEwAccessibility")
    private val ontouchListener = VIEw.OntouchListener { _,motionEvent ->
        if(swipeposition < 0) return@OntouchListener false
        val point =  Point(motionEvent.rawX.toInt(),motionEvent.rawY.toInt())
        val @R_403_3837@Holder = recyclerVIEw.findVIEwHolderForAdapterposition(swipeposition)
        val swipedItem = @R_403_3837@Holder!!.itemVIEw
        val rect = Rect()
        swipedItem.getGlobalVisibleRect(rect)

        if(motionEvent.action == MotionEvent.ACTION_DOWN ||
            motionEvent.action == MotionEvent.ACTION_MOVE ||
                motionEvent.action == MotionEvent.ACTION_UP){
            if(rect.top < point.y && rect.bottom > point.y)
                gestureDetector.ontouchEvent(motionEvent)
            else {
                removeQueue.add(swipeposition)
                swipeposition = -1
                recoverSwipeItem()
            }
        }
        false
    }
    @Synchronized
    private fun recoverSwipeItem() {
        while(!removeQueue.isEmpty()) {
            val pos = removeQueue.poll()!!.toInt()
            if(pos > -1){
                recyclerVIEw.adapter!!.notifyItemChanged(pos)
            }
        }
    }

    init{
        this.buttonList = ArrayList()
        this.gestureDetector = GestureDetector(context,gestureListener)
        this.recyclerVIEw.setontouchListener(ontouchListener)
        this.removeQueue = IntlinkedList()

        attachSwipe()
    }

    private fun attachSwipe() {
        val itemtouchhelper = itemtouchhelper(this)
        itemtouchhelper.attachToRecyclerVIEw(recyclerVIEw)
    }

    class IntlinkedList : linkedList<Int>() {
        overrIDe fun contains(element: Int): Boolean {
            return false
        }

        overrIDe fun lastIndexOf(element: Int): Int {
            return element
        }

        overrIDe fun remove(element: Int): Boolean {
            return false
        }

        overrIDe fun indexOf(element: Int): Int {
            return element
        }

        overrIDe fun add(element: Int): Boolean {
            return if(contains(element))
                false
            else super.add(element)
        }
    }

    overrIDe fun onMove(
        recyclerVIEw: RecyclerVIEw,vIEwHolder: RecyclerVIEw.VIEwHolder,target: RecyclerVIEw.VIEwHolder
    ): Boolean {
        dragAdapter.swAPItems(vIEwHolder.adapterposition,target.adapterposition)
        return true
    }

    overrIDe fun onSwiped(vIEwHolder: RecyclerVIEw.VIEwHolder,direction: Int) {
        val pos= vIEwHolder.adapterposition
        if(swipeposition != pos)removeQueue.add(swipeposition)
        swipeposition = pos
        if(buttonBuffer.containsKey(swipeposition))buttonList = buttonBuffer[swipeposition]
        else buttonList!!.clear()
        buttonBuffer.clear()
        swipeThreshold = 0.5f * buttonList!!.size.tofloat() * buttonWIDth.tofloat()
        recoverSwipeItem()
    }

    overrIDe fun getSwipeThreshold(vIEwHolder: RecyclerVIEw.VIEwHolder): float {
        return swipeThreshold
    }

    overrIDe fun getSwipeEscapeVeLocity(defaultValue: float): float {
        return 0.1f * defaultValue
    }

    overrIDe fun getSwipeVeLocityThreshold(defaultValue: float): float {
        return 5.0f*defaultValue
    }

    overrIDe fun onChildDraw(
        c: Canvas,recyclerVIEw: RecyclerVIEw,dX: float,dY: float,actionState: Int,isCurrentlyActive: Boolean
    ) {
        val pos = vIEwHolder.adapterposition
        var translationX = dX
        var itemVIEw = vIEwHolder.itemVIEw
        if(pos<0){
            swipeposition = pos
            return
        }
        if(actionState == itemtouchhelper.ACTION_STATE_SWIPE)
        {
            if(dX <0){
                var buffer : MutableList<Swipebutton> = ArrayList()
                if(!buttonBuffer.containsKey(pos)){
                    instantiateMybutton(vIEwHolder,buffer)
                    buttonBuffer[pos] = buffer
                }else{
                    buffer = buttonBuffer[pos]!!
                }
                translationX = dX * buffer.size.tofloat() * buttonWIDth.tofloat() / itemVIEw.wIDth
                drawbutton(c,itemVIEw,buffer,pos,translationX)
            }
        }
        super.onChildDraw(c,recyclerVIEw,vIEwHolder,translationX,dY,actionState,isCurrentlyActive)
    }

    private fun drawbutton(c: Canvas,itemVIEw: VIEw,buffer: MutableList<Swipebutton>,pos: Int,translationX: float) {
        var right = itemVIEw.right.tofloat()
        val dbuttonWIDth = -1 * translationX/buffer.size
        for(button in buffer){
            val left = right - dbuttonWIDth
            button.onDraw(c,RectF(left,itemVIEw.top.tofloat(),right,itemVIEw.bottom.tofloat()),pos)
            right = left
        }
    }

}

Swipebutton.kt

package com.softsquared.template.kotlin.src.main.today

import androID.content.Context
import androID.content.res.Resources
import androID.graphics.*
import androID.graphics.drawable.BitmapDrawable
import androID.graphics.drawable.Drawable
import androIDx.core.content.ContextCompat

class Swipebutton
    (private val context: Context,private val text:String,private val textSize:Int,private val imageResID:Int,private val color:Int,private val Listener: SwipebuttonClickListener) {

    private val resources:Resources
    private var pos:Int = 0
    private var clickRegion: RectF?=null

    init{
        resources = context.resources
    }

    fun onClick(x:float,y:float):Boolean{
        if(clickRegion != null && clickRegion !!.contains(x,y))
        {
            Listener.onClick(pos)
            return true
        }
        return false
    }

    fun onDraw(c: Canvas,rectF:RectF,pos:Int){
        val p = Paint()
        p.color = color
        c.drawRect(rectF,p)

        p.color = color.WHITE
        p.textSize = textSize.tofloat()

        val r = Rect()
        val cHeight = rectF.height()
        val cWIDth = rectF.wIDth()
        p.textAlign = Paint.Align.left
        p.getTextBounds(text,text.length,r)
        var x = 0f
        var y = 0f
        if(imageResID == 0){
            x = cWIDth/2f-r.wIDth()/2f-r.left.tofloat()
            y = cHeight/2f +r.height()/2f-r.bottom.tofloat()
            c.drawText(text,rectF.left+x,rectF.top+y,p)

        }else{
            val d = ContextCompat.getDrawable(context,imageResID)
            val bitmap = drawabletoBitmap(d)
            c.drawBitmap(bitmap,(rectF.left+rectF.right)/2,(rectF.top+rectF.bottom)/2,p)
        }

        clickRegion = rectF
        this.pos = pos
    }

    private fun drawabletoBitmap(d: Drawable?): Bitmap {
        if(d is BitmapDrawable)return d.bitmap
        val bitmap = Bitmap.createBitmap(d!!.intrinsicWIDth,d.intrinsicHeight,Bitmap.Config.ARGB_8888)
//        val bitmap = Bitmap.createBitmap(25,25,Bitmap.Config.ARGB_8888)

        val canvas = Canvas(bitmap)
//        d!!.setBounds(0,canvas.wIDth,canvas.height)
        d!!.setBounds(0,50,50)

        d!!.draw(canvas)
        return bitmap
    }
}

我的片段代码是

class TodayFragment :
    BaseFragment<FragmentTodayBinding>(FragmentTodayBinding::bind,R.layout.fragment_today),TodayVIEw
{
    val memoList:ArrayList<MemoItem> = arraylistof()
    lateinit var todayMemoAdapter:MemoAdapter

    overrIDe fun onVIEwCreated(vIEw: VIEw,savedInstanceState: Bundle?) {
        super.onVIEwCreated(vIEw,savedInstanceState)


        showLoadingDialog(context!!)
        TodayService(this).onGetScheduleItems()

        // adapter
        val mLayoutManager = linearlayoutmanager(context)
        todayMemoAdapter = MemoAdapter(memoList,context!!){
            val scheduleDetailDialog = ScheduleDetailDialog(context!!)
            scheduleDetailDialog.setonModifyBtnClickedListener {
                (activity as MainActivity).showBottomAddScheduleSheetDialog()
            }
            scheduleDetailDialog.start(it)
        }
        binding.todayRecyclerVIEw.apply {
            layoutManager = mLayoutManager
            adapter = todayMemoAdapter
        }

        binding.todayImageNoItem.setonClickListener {
            (activity as MainActivity).showBottomAddScheduleSheetDialog()

        }

        // swipe and drag
        val swipe = object: MemoSwipeHelper(todayMemoAdapter,context!!,binding.todayRecyclerVIEw,200)
        {
            overrIDe fun instantiateMybutton(
                vIEwHolder: RecyclerVIEw.VIEwHolder,buffer: MutableList<Swipebutton>
            ) {
                // add button
                buffer.add(Swipebutton(context!!,"Delete",30,R.drawable.schedule_delete,color.parsecolor("#32363C"),object: SwipebuttonClickListener {
                        overrIDe fun onClick(pos: Int) {
                            showCustomToast("delete $pos")
                        }
                    }
                    ))
                buffer.add(Swipebutton(context!!,"Share",R.drawable.schedule_share,color.parsecolor("#FFAE2A"),object: SwipebuttonClickListener {
                        overrIDe fun onClick(pos: Int) {
                            showCustomToast("share $pos")
                        }
                    }
                ))
            }

        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的如何在kotlin中以编程方式更改形状按钮半径并填充recyclerview项目?全部内容,希望文章能够帮你解决如何在kotlin中以编程方式更改形状按钮半径并填充recyclerview项目?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: