2017-07-19 1 views
1

J'ai une application Android qui a deux RecyclerViews intégrés dans un HorizontalScrollView. Le premier RecyclerView a des catégories et lorsque vous sélectionnez une catégorie, le RecyclerView inférieur répertorie les éléments de cette catégorie. Pour détecter la catégorie sélectionnée, j'essaie d'utiliser l'événement Touch. Lorsqu'un EditText obtient l'événement tactile, cette ligne est sélectionnée. Cette partie fonctionne très bien. Le problème est que je dois aussi pouvoir éditer certains champs dans la liste Catégorie. Si je lier l'événement Touch sur un EditText, le EditText n'obtient pas le focus lorsque vous cliquez dessus. J'ai essayé d'utiliser l'événement FocusChanged à la place, mais parfois, lors du défilement horizontal, le focus sur le haut RecyclerView est défini sur l'EditText pour la première catégorie, provoquant ainsi la modification de la catégorie sélectionnée. Y a-t-il un moyen de lier l'événement Touch dans MvvmCross tout en le laissant provoquer le focus du contrôle lié?Événement tactile de liaison MvvmCross évitant de mettre l'accent sur MvxItemTemplate

C'est la mise en page pour mon modèle d'objet:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="2dp" 
    android:paddingBottom="2dp" 
    local:MvxBind="This View"> 
    <TextView 
     android:layout_width="300dp" 
     android:layout_height="wrap_content" 
     style="@style/TableEditText" 
     local:MvxBind="Text Name; LongClick Name_LongClick; Touch Touch" 
     android:id="@+id/txtName" /> 
    <EditText 
     android:layout_width="110dp" 
     android:layout_height="wrap_content" 
     style="@style/TableEditText" 
     android:inputType="numberDecimal|numberSigned" 
     android:selectAllOnFocus="true" 
     local:MvxBind="Text Cubes, Converter=FloatToStringConverter; Touch Touch" 
     android:id="@+id/txtCubes" /> 
    <EditText 
     android:layout_width="110dp" 
     android:layout_height="wrap_content" 
     style="@style/TableEditText" 
     android:inputType="numberDecimal|numberSigned" 
     android:selectAllOnFocus="true" 
     local:MvxBind="Text Weight, Converter=IntToStringConverter; Touch Touch" 
     android:id="@+id/txtWeight" /> 
    <EditText 
     android:layout_width="500dp" 
     android:layout_height="wrap_content" 
     style="@style/TableEditText" 
     android:inputType="textCapSentences" 
     local:MvxBind="Text Note; Touch Touch" 
     android:id="@+id/txtNote" /> 
    <ImageButton 
     android:src="@drawable/ic_action_delete" 
     android:scaleType="fitCenter" 
     android:padding="3dp" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_gravity="center" 
     local:MvxBind="Click DeleteClicked" 
     android:id="@+id/btnDeleteCategory" /> 
</LinearLayout> 

Ceci est mon crochet pour l'événement tactile:

public MvxCommand Touch 
    { 
     get 
     { 
      return new MvxCommand(() => 
      { 
       SurveyView act = (Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity as SurveyView) ?? null; 
       if (act != null) 
       { 
        act.SetCurrCategory(this); 
       } 
      }); 
     } 
    } 

Ceci est ma liaison personnalisée pour l'événement Touch:

public class MvxViewTouchBinding 
    : MvxAndroidTargetBinding 
{ 
    private readonly View _view; 
    private IMvxCommand _command; 

    public MvxViewTouchBinding(View view) : base(view) 
    { 
     _view = view; 
     _view.Touch += ViewOnTouch; 
    } 

    private void ViewOnTouch(object sender, View.TouchEventArgs eventArgs) 
    { 
     if (_command != null) 
     { 
      _command.Execute(); 
     } 
    } 

    public override void SetValue(object value) 
    { 
     _command = (IMvxCommand)value; 
    } 

    protected override void Dispose(bool isDisposing) 
    { 
     if (isDisposing) 
     { 
      _view.Touch -= ViewOnTouch; 
     } 
     base.Dispose(isDisposing); 
    } 

    protected override void SetValueImpl(object target, object value) 
    { 
    } 

    public override Type TargetType 
    { 
     get { return typeof(IMvxCommand); } 
    } 

    public override MvxBindingMode DefaultMode 
    { 
     get { return MvxBindingMode.OneWay; } 
    } 
} 

Ceci est dans le Setup.cs

 registry.RegisterCustomBindingFactory<View>("Touch", 
                view => new MvxViewTouchBinding(view)); 
+1

Essayez ceci - https://stackoverflow.com/a/36360795/2754727 – pnavk

+2

Essayez de définir 'eventArgs.Handled = false;' – Cheesebaron

+0

@Cheesebaron C'est fait! Merci fromage! –

Répondre

0

je devais changer le gestionnaire ViewOnTouch à:

private void ViewOnTouch(object sender, View.TouchEventArgs eventArgs) 
    { 
     eventArgs.Handled = false; 

     if (_command != null) 
     { 
      _command.Execute(); 
     } 
    } 

par défaut Apparemment Handled vrai et cela provoque la chaîne d'événements pour terminer après celui-ci est appelé. Le paramétrer sur false indique au système que l'événement n'a pas encore été géré correctement, il appelle donc le prochain gestionnaire en ligne. Je devine qu'un gestionnaire ultérieur met l'accent. Merci @Cheesebaron