P
P
PoisonousHeart2021-08-06 15:02:40
Android
PoisonousHeart, 2021-08-06 15:02:40

No data change on UI?

Extended FrameLayout . Associated some layout with this custom View. I want to use it to display data through a custom property (BindingAdapter is used for this, because in the future one of the classes that implements some interface will be thrown there, so attr.xml is not suitable). The use of this custom View is planned through xml, so managing it from code is not good. Ideally, the adapter binding is triggered and custom logic is performed on the data in it and inside the component states change to the layout that is associated with this custom View in the constructor. The use of this custom View is also planned in Recyclerplease take this into account. For simplicity of the example, I pulled out a custom View from the Recycler and threw it just on the fragment. It also doesn't work. If you add static text (then it is displayed), that is, there are no changes on the ui that should occur when the binding is triggered, but the binding mode works through debugging.

Custom View code:

class RecalculateView : FrameLayout {
    lateinit var binding: ViewRecalculateLayoutBinding
    lateinit var name: String

    constructor(context: Context) : super(context){
        initView(context)
    }
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){
        initView(context)
    }
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr){
        initView(context)
    }



    private fun initView(context: Context){// Связать файл разметки с текущим View
        inflate(context, R.layout.view_recalculate_layout, this) // связать xml и view
        binding = ViewRecalculateLayoutBinding.inflate(
            LayoutInflater.from(context)
        )
    }
}

@BindingAdapter(value = ["setName"], requireAll = true)
fun <ITEM: String> RecalculateView.name(item: ITEM?){
    item?.let{
        binding.tex.text = it
    }
}


view_recalculate_layout.xml layer code associated with it :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
            android:id="@+id/tex"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="@color/black"/>
</layout>


fragment markup code on which the display should take place:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ru.startex.app.basic_recalculate.RecalculateView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:setName="@{`it is hell`}"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PoisonousHeart, 2021-08-07
@PoisonousHeart

1. Inside view:

binding = ViewRecalculateLayoutBinding.inflate(
            LayoutInflater.from(context),
            this,
            true
        )

2. Start interface:
interface Childs {
    fun getTextView(): TextView
}

3. We inherit from the View Interface
class RecalculateView :  Childs, LinearLayoutCompat{
...
}

4. Redefine:
...
override fun getTextView() = binding.tex
...
}

5. Well Adapter:
@BindingAdapter(value =["customText"], requireAll = false)
fun <T: String> setCustom(view: RecalculateView ?, item: T?){
    item?.let{
        view?.getTextView()?.text = it
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question