在自定义视图中设置数据

问题描述

我正在构建一个自定义视图,该视图每6秒钟向Rest API发出一次HTTP请求,并显示一个不同的问题及其可能的答案。在这种情况下,Buff就是包含问题及其可能答案的对象。

现在,将插入视图,但是,在设置数据时(请参见setAnswer函数),第一个视图显示答案列表最后一个元素中的文本。其余视图不显示任何文本。

从API接收到的Buff对象

有了这些数据,我应该显示一个具有3个可能答案的问题:“没有目标!”,“一个目标!”,“两个或更多”的顺序。

{
    "result": {
        "id": 1,"author": {
            "first_name": "Ronaldo"
        },"question": {
            "id": 491,"title": "Kaio Jorge has 4 goals this tournament – I think he will score again today. What do you think?"
        },"answers": [
            {
                "id": 1163,"buff_id": 0,"title": "No goals!"
            },{
                "id": 1164,"title": "One goal!"
            },{
                "id": 1165,"title": "Two or more!"
            }
        ]
    }
}

这是目前的显示方式 First element displays the text from the 3rd answer and the other two are empty

BuffView.kt(我的自定义视图)

class BuffView(context: Context,attrs: AttributeSet? = null): LinearLayout(context,attrs) {

    private val apiErrorHandler = ApiErrorHandler()
    private val getBuffUseCase = GetBuffUseCase(apiErrorHandler)

    private var buffIdCount = 1

    private val buffView: View

    init {
        buffView = inflate(context,R.layout.buff_view,this)
    }

    fun start() {
        getBuff()
    }

    private fun getBuff() {
        getBuffUseCase.invoke(buffIdCount.toLong(),object : UseCaseResponse<Buff> {
            override fun onSuccess(result: Buff) {
                displayBuff(result)
            }

            override fun onError(errorModel: ErrorModel?) {
                //Todo: show error toast
                Log.e("AppDebug","onError: errorModel $errorModel")
            }
        })

        val delay = 6000L
        RepeatHelper.repeatDelayed(delay) {
            if (buffIdCount < 5) {
                buffIdCount++
                getBuff()
            }
        }
    }

    private fun displayBuff(buff: Buff) {
        setQuestion(buff.question.title)
        setAuthor(buff.author)
        setAnswer(buff.answers)
        setCloseButton()
        buffView.visibility = VISIBLE
    }

    private fun setQuestion(questionText: String) {
        question_text.text = questionText
    }

    private fun setAuthor(author: Buff.Author) {
        val firstName = author.firstName
        val lastName = author.lastName
        sender_name.text = "$firstName $lastName"

        Glide.with(buffView)
            .load(author.image)
            .into(sender_image)
    }

    private fun setAnswer(answers: List<Buff.Answer>) {
        val answersContainer = findViewById<LinearLayout>(R.id.answersContainer)
        answersContainer.removeAllViews()
        for(answer in answers) {
            val answerView: View = LayoutInflater.from(answersContainer.context).inflate(
                R.layout.buff_answer,answersContainer,false
            )
            answer_text?.text = answer.title

            answersContainer.addView(answerView)
        }
    }

    private fun setCloseButton() {
        buff_close.setonClickListener {
            buffView.visibility = GONE
        }
    }
}

buff_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include layout="@layout/buff_sender"/>
    <include layout="@layout/buff_question"/>

    <LinearLayout
        android:id="@+id/answersContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"/>

</LinearLayout>

buff_answer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:background="@drawable/light_bg"
    android:orientation="horizontal"
    tools:ignore="RtlHardcoded">

    <ImageView
        android:id="@+id/answer_image"
        android:layout_width="27dp"
        android:layout_height="27dp"
        android:layout_gravity="center_vertical"
        android:src="@drawable/ic_generic_answer"
        android:padding="4dp" />

    <TextView
        android:id="@+id/answer_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:padding="4dp"
        android:textColor="@color/test_color_dark"
        tools:text="The keeper's right"
        android:textStyle="bold" />
</LinearLayout>

解决方法

尝试在invalidate()之后调用addView(),以告知视图应重新呈现。另外,您需要在answerView上设置文本,因此该方法应如下所示:

private fun setAnswer(answers: List<Buff.Answer>) {
        val answersContainer = findViewById<LinearLayout>(R.id.answersContainer)
        answersContainer.removeAllViews()
        for(answer in answers) {
            val answerView: View = LayoutInflater.from(answersContainer.context).inflate(
                R.layout.buff_answer,answersContainer,false
            )
            answerView.answer_text?.text = answer.title

            answersContainer.addView(answerView)
        }
        invalidate() // or invalidateSelf()
    }

另一件事是,从自定义视图调用api是一种非常糟糕的做法。执行用例应该在ViewModel / Presenter等级别。然后应该将buff提供给“片段/活动”,然后在CustomView中进行设置。您也不会在诸如onDetachedFromWindow()之类的破坏视图的回调中取消请求,这可能导致内存泄漏

,

在不重新创建同一视图的情况下,不能多次添加同一视图,这并不像人们想的那么简单。

private fun setAnswer(answers: List<Buff.Answer>) {
    val answersContainer = findViewById<LinearLayout>(R.id.answersContainer)
    var viewlist = ArrayList()
    answersContainer.removeAllViews()
    for(answer in answers) {
        val answerView: View = LayoutInflater.from(answersContainer.context).inflate(
            R.layout.buff_answer,false
        )
        answer_text?.text = answer.title
        viewlist.add(answerView)
        
    }
    for(view in viewlist)
        answersContainer.addView(view)
}