I'm trying to use Custom view that extends RelativeLayout
and named CardView
. A added some attr and created XML file for them, everything work correct, except onClick
. So, at first I've created a special class for my view:
class CardView(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {
init {
inflate(context, R.layout.card_view, this)
val imageView: ImageView = findViewById(R.id.image)
val textView: TextView = findViewById(R.id.caption)
val line: ImageView = findViewById(R.id.line)
val attributes = context.obtainStyledAttributes(attrs, R.styleable.CardView)
imageView.setImageResource(attributes.getResourceId(R.styleable.CardView_image, 0))
textView.text = attributes.getString(R.styleable.CardView_text)
line.setBackgroundColor(attributes.getColor(R.styleable.CardView_lineColor, 0))
attributes.recycle()
} }
I aslo have a xml file for my View:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/cardSize"
android:layout_height="@dimen/cardSize"
android:background="@drawable/card_color_selector"
android:clickable="true">
<ImageView
android:id="@+id/image"
android:layout_width="60sp"
android:layout_height="60sp"
android:layout_centerHorizontal="true"
android:layout_margin="3sp"
tools:src="@color/colorPrimary" />
<TextView
android:id="@+id/caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/text_normal"
android:textSize="15dp"
android:layout_alignParentBottom="true"
android:layout_margin="10sp"
android:gravity="center"
android:textColor="@color/darkGray"
tools:text="Caption" />
<ImageView
android:id="@+id/line"
android:layout_width="match_parent"
android:visibility="visible"
android:layout_height="5dp"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary" />
</RelativeLayout>
And finally I've added my customView
in activity layout with link to onClick
method in view model like here:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.presentation.screen.HomeViewModel" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.project.presentation.utils.CardView
android:id="@+id/card"
android:layout_width="@dimen/cardSize"
android:layout_height="@dimen/cardSize"
android:clickable="true"
android:onClick="@{() -> viewModel.onClick()}"
app:image="@drawable/icon"
app:lineColor="@color/colorPrimary"
app:text="@string/text" />
</android.support.constraint.ConstraintLayout>
</layout>
So everything is good, colors are changed as i'd written in card_color_selector.xml
, but method onClick
in my ViewModel
has never called. Oh, by the way my view model is here, now I just want to logged all the clicks:
class HomeViewModel : BaseViewModel<Router>() {
fun onClick() {
Log.e("myLog", "HomeViewModel onClick")
}
}
I've tried everything! Please help me to figure out what I'm doing wrong.