J'essaie d'utiliser la vue personnalisée qui étend RelativeLayout et nommée CardView . A ajouté quelques attr et créé un fichier XML pour eux, tout fonctionne correctement, sauf onClick . Donc, au début, j'ai créé une classe spéciale pour ma vue:
class HomeViewModel : BaseViewModel<Router>() {
fun onClick() {
Log.e("myLog", "HomeViewModel onClick")
}
}
J'ai également un fichier xml pour ma vue:
<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>
Et enfin, j'ai ajouté mon customView dans la mise en page de l'activité avec un lien vers la méthode onClick dans le modèle de vue comme ici:
<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>
Donc tout va bien, les couleurs sont changées comme je l'avais écrit dans card_color_selector.xml , mais la méthode onClick dans mon ViewModel n'a jamais appelé . Oh, au fait que mon modèle de vue est ici, maintenant je veux juste enregistrer tous les clics:
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()
} }
J'ai tout essayé! S'il vous plaît, aidez-moi à comprendre ce que je fais de mal.
4 Réponses :
Dans votre section
fun onClick() {
Log.e("myLog", "HomeViewModel onClick")
}
passer View In XML pour
onClick = "method", vous devez définir le paramètre comme classe
Viewdans votre méthode.
Vous pouvez voir ceci
Changez votre méthode onClick comme ceciEssayez comme ceci
fun onClick(view: View) {
Log.d("myLog", "HomeViewModel onClick")
}
Malheureusement, j'ai déjà essayé de cette façon, cela ne change rien :(
Si cela ne vous dérange pas, puis-je vous demander une chose pourquoi vous utilisez Log.e au lieu de Log.d ou autres ?? Avez-vous correctement vérifié le journal?
oui, car il y a un autre journal dans mon application, donc je n'ai aucun problème avec eux.
il semble que je doive définir onClickListener sur ma vue personnalisée, mais dans l'architecture mvvm, je ne peux rien utiliser comme findViewById . C'est pourquoi j'essaie de trouver une autre solution.
Je n'ai pas assez de réputation pour commenter, alors pouvez-vous essayer
fun onClick(view:View) {
Log.e("myLog", "HomeViewModel onClick")
}
et pour la fonction
android:onClick="@{(v) -> viewModel.onClick(v)}"
Oui, j'ai déjà essayé et je ne sais pas pourquoi cela ne fonctionne pas. Le problème pourrait-il être lié à l'attribut android: clickable = "true" pour la mise en page dans ma vue personnalisée?
Peut-être que le seul moyen de le savoir est de le tester. supprimez les deux attributs vrai cliquables et testez-les. et assurez-vous que vous avez défini la valeur de homeViewModel.
J'ai eu le même problème.
Vérifiez l'attribut cliquable de votre vue personnalisée!
Si la vue de votre enfant dans la vue personnalisée empêche l'événement tactile, celle de vos parents
android: onClick sera ignoré.