Избегайте круговых ссылок между счетчиком и адаптером в android (kotlin) - PullRequest
0 голосов
/ 14 января 2020

для разметки. Я создал пользовательское представление, состоящее из счетчика, чье представление имеет TextInputLayout и EditText, но мне нужно, чтобы EditText не был редактируемым и в то же время прослушивал * 1004. * несмотря на это, я добавил вид выше к intercept event.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/input_text"
        style="@style/Tp.TextInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/edit_text"
            style="@style/TextRegular"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableEnd="@drawable/ic_off"/>
    </com.google.android.material.textfield.TextInputLayout>


    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/click_surface"
        app:layout_constraintBottom_toBottomOf="@+id/input_text"
        app:layout_constraintEnd_toEndOf="@+id/input_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/input_text" />

</androidx.constraintlayout.widget.ConstraintLayout>

Теперь мне нужно нажать на представление "click_surface", чтобы вызвать событие спиннера onClick, поэтому я передал спиннер к его адаптеру

class MySpinnerView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr){

    private val spinner: AppCompatSpinner by lazy { spinner_view }

    init {
        LayoutInflater.from(context).inflate(R.layout.hint_input_spinner_view, this)
        spinner.adapter = InputTextSpinnerAdapter(context, uiModel.choices, spinner, uiModel.upperDescription, uiModel.hint)
    }
}

и затем в getView адаптера

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.hint_input_spinner_root_view,parent,false)
        ...
        view.click_surface.setOnClickListener {
            spinner.performClick()
        }

        return view
    }

Но теперь у меня есть ссылка циклическая c, которая может вызвать утечку памяти. Как я мог решить это?

...