Use viewbinding with custom views

This commit is contained in:
Shinokuni 2020-07-13 19:33:11 +02:00
parent 2bc5438348
commit 442c257593
2 changed files with 7 additions and 11 deletions

View File

@ -2,26 +2,22 @@ package com.readrops.app.utils
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView
import com.readrops.app.R import com.readrops.app.R
import com.readrops.app.databinding.EmptyListViewBinding
/** /**
* A simple custom view to display a empty list message * A simple custom view to display a empty list message
*/ */
class EmptyListView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) { class EmptyListView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
init { val binding: EmptyListViewBinding = EmptyListViewBinding.inflate(LayoutInflater.from(context), this, true)
// no binding here, it makes the view rendering fail
View.inflate(context, R.layout.empty_list_view, this)
val imageView: ImageView = findViewById(R.id.empty_list_image)
val textView: TextView = findViewById(R.id.empty_list_text_v)
init {
val attributes = context.obtainStyledAttributes(attrs, R.styleable.EmptyListView) val attributes = context.obtainStyledAttributes(attrs, R.styleable.EmptyListView)
imageView.setImageDrawable(attributes.getDrawable(R.styleable.EmptyListView_image)) binding.emptyListImage.setImageDrawable(attributes.getDrawable(R.styleable.EmptyListView_image))
textView.text = attributes.getString(R.styleable.EmptyListView_text) binding.emptyListText.text = attributes.getString(R.styleable.EmptyListView_text)
attributes.recycle() attributes.recycle()
} }

View File

@ -15,7 +15,7 @@
tools:src="@drawable/ic_rss_feed_grey" /> tools:src="@drawable/ic_rss_feed_grey" />
<TextView <TextView
android:id="@+id/empty_list_text_v" android:id="@+id/empty_list_text"
style="@style/TextAppearance.AppCompat.Large" style="@style/TextAppearance.AppCompat.Large"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"