2016-03-31 34 views
2

Ich entwickle eine App mit MvvmCross und ich habe Probleme in der Android-App, die die Bindung einrichten, um einen Klickvorgang in zwei verschiedenen Steuerelementen zu erfassen, die ein MvxListView-Element bilden. Die Bindung für alle Daten innerhalb des Ansichtsmodell alles funktioniert gut Hier ist die AXML für meine FavouritesViewMvvmCross - MvxListView mehrere Klicks binden

<?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="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white"> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/LightGrey" 
     android:scrollbars="vertical" 
     android:scrollbarStyle="insideOverlay"> 
     <LinearLayout 
       REMOVED FOR CLARITY /> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 
      <Button 
       style="@style/ButtonFont" 
       android:text="Back" 
       android:layout_width="80dp" 
       android:layout_height="43dp" 
       android:layout_marginTop="15dp" 
       android:layout_marginLeft="12dp" 
       android:background="@drawable/darkgrey_rounded_button" 
       local:MvxBind="Text BackButtonText; Click CloseFragmentCommand" 
       android:id="@+id/buttonBackFavourites" 
       android:drawableLeft="@drawable/arrow_left" 
       android:padding="8dp" /> 
      <TextView 
       style="@style/TitleFont" 
       android:text="Title" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="12dp" 
       android:layout_marginLeft="12dp" 
       local:MvxBind="Text TitleText" 
       android:id="@+id/textTitleFavouritess" /> 
      <Mvx.MvxListView 
       local:MvxBind="ItemsSource Favourites; ItemClick ShowFavouriteCommand" 
       local:MvxItemTemplate="@layout/item_favourite" 
       android:divider="@color/LightGrey" 
       android:dividerHeight="10dp" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginTop="30dp" 
       android:scrollbars="none" 
       android:id="@+id/listFavourites" /> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

Und die MvxItemTemplate ist hier:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/MidGrey"> 
    <TextView 
     style="@style/GeneralFont" 
     android:id="@+id/favItemText" 
     android:text="Text line2 " 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="30dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:textColor="@color/DarkGrey" 
     android:lines="2" 
     local:MvxBind="Text Title" /> 
    <ImageView 
     android:id="@+id/favItemImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="24dp" 
     android:layout_marginRight="10dp" 
     android:tint="@color/DarkGrey" 
     android:src="@drawable/arrow_right" /> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:id="@+id/viewFavSpacer" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="8dp" 
     android:layout_marginBottom="8dp" 
     android:layout_below="@+id/favItemText" 
     android:background="@color/DarkGrey" /> 
    <Button 
     android:text="(x) Remove" 
     android:layout_below="@+id/viewFavSpacer" 
     android:layout_width="wrap_content" 
     android:layout_height="30dp" 
     android:textColor="@color/DarkGrey" 
     android:textSize="12dp" 
     android:background="@color/MidGrey" 
     local:MvxBind="Text 'RemoveFromFavourites', Converter=UseTextService; Click DeleteFavouriteMessage(.)" 
     android:layout_marginBottom="8dp" 
     android:layout_marginLeft="10dp" /> 
</RelativeLayout> 

Hier ist meine FavouritesViewModel:

using System; 
using Cirrious.MvvmCross.Plugins.Messenger; 
using MyApp.Core.Managers; 
using MyApp.Core.Services; 
using System.Collections; 
using MyApp.Core.Domain; 
using System.Collections.Generic; 
using Cirrious.MvvmCross.ViewModels; 
using System.Collections.ObjectModel; 
using System.ServiceModel.Channels; 
using Cirrious.CrossCore; 
using MyApp.Core.Messages; 
using MyApp.Core.Helpers; 

namespace MyApp.Core.ViewModels 
{ 
    public class FavouritesViewModel: StandardsViewModel 
    { 
     private IFavouritesService _favouritesService; 

     public FavouritesViewModel (ITextService textService, IMvxMessenger messenger, ISettingsManager settingsManager, IPageService pageService, IFavouritesService favouriteService): base(textService, messenger, settingsManager, pageService) 
     { 
      _logger.LeaveBreadcrumb ("FavouritesViewModel" , "Constructor"); 

      _favouritesService = favouriteService; 
     } 

     public string TitleText { get { return _textService.GetString("Favourites"); } } 


     private ObservableCollection<Favourite> _favourites; 
     public ObservableCollection<Favourite> Favourites 
     { 
      get { return _favourites; } 
      set { 
       _favourites = value; 
       RaisePropertyChanged (()=>Favourites); 
      } 
     } 

     public async new void Init() 
     { 
      _logger.LeaveBreadcrumb ("FavouritesViewModel" , "Init"); 

      var messenger = Mvx.Resolve<IMvxMessenger>(); 
      messenger.Subscribe<DeleteFavouriteMessage>(message => { 
       DeleteFavourite (message.ThisFavourite); 
      }); 

      var favs = await _favouritesService.GetAll(); 
      _logger.LeaveBreadcrumb ("FavouritesViewModel:Init", "Favourites found:" + favs.Count.ToString()); 
      Favourites = new ObservableCollection<Favourite> (favs); 
     } 


     /// <summary> 
     /// Deletes the favourite. 
     /// </summary> 
     /// <param name="favourite">Favourite.</param> 
     public void DeleteFavourite(Favourite favourite) 
     { 
      _logger.LeaveBreadcrumb ("FavouritesViewModel:DeleteFavourite", favourite.Title); 

      Favourites.Remove (favourite); 
      RaisePropertyChanged ("Favourites"); 
      _favouritesService.Delete (favourite); 
     } 


     /// <summary> 
     /// The delete favourite command. 
     /// </summary> 
     private MvxCommand<Favourite> _deleteFavouriteCommand; 

     /// <summary> 
     /// Gets the delete favourite command. 
     /// </summary> 
     /// <value>The delete favourite command.</value> 
     public MvxCommand<Favourite> DeleteFavouriteCommand 
     { 
      get 
      { 
       _deleteFavouriteCommand = _deleteFavouriteCommand ?? new MvxCommand<Favourite> (DeleteFavourite); 
       return _deleteFavouriteCommand; 
      } 
     } 


     public void ShowFavourite(Favourite favourite) 
     { 
      if (favourite != null) 
      { 
       _logger.LeaveBreadcrumb (string.Format("FavouriteViewModel - Selected : {0}", favourite.Title)); 
       // TODO need to display the favourite piece of info 
      } 
     } 

     private MvxCommand<Favourite> _showFavouriteCommand; 
     public MvxCommand<Favourite> ShowFavouriteCommand 
     { 
     get 
      { 
       _showFavouriteCommand = _showFavouriteCommand ?? new MvxCommand<Favourite> (ShowFavourite); 
       return _showFavouriteCommand; 
      } 
     } 

     public string ButtonRemoveText { get { return _textService.GetString ("RemoveFromFavourites"); } } 
     public string AreYouSureText { get { return _textService.GetString ("AreYouSure"); } } 
     public string RemoveFavouriteText { get { return _textService.GetString ("RemoveFavourite"); } } 
    } 
} 

Die Symptome meines Problems sind die Ansicht empfängt die DeleteFavouriteMessage, wenn auf die Schaltfläche "(x) Entfernen" geklickt wird. Wenn auf das Element geklickt wird, wird das ItemClick-Ereignis ShowFavouriteCommand nicht aus der MvxListView ausgelöst.

Ich bin mir sicher, dass ich etwas mache oder etwas Einfaches verpasse, aber ich habe Mühe, irgendwelche Fortschritte zu machen.

+0

Wie sieht Ihr FavouritesViewModel aussehen? –

+0

Ich habe den ViewModel-Code wie gewünscht hinzugefügt :) – JDibble

Antwort

3

Ich habe dies zuvor begegnet. Die Schaltfläche in Ihrem MvxItemTemplate Layout ist Fokus stehlen. Versuchen Sie android:focusable="false" für die Schaltfläche in Ihrem Layout festzulegen.

Changing focus from Button in ListView row to list item

ich diese Antwort bin Aktualisierung gründlicher zu sein. Bei der Einstellung android:focusable="false" für eine allgemeine Schaltfläche Steuerelement in einem ListView Element behebt das Problem. Wenn Sie eine ImageButton verwenden, wird dies nicht funktionieren.

Für eine ImageButton müssen Sie android:descendantFocusability="blocksDescendants" in der Stammansicht Ihres Layouts festlegen.

Siehe die akzeptierte Antwort hier: can't click on listview row with imagebutton

+1

Vielen Dank @pnavk Sie haben das Problem gelöst – JDibble

0

Ich glaube, Sie fehlen android:choiceMode="singleChoice"

<Mvx.MvxListView 
       local:MvxBind="ItemsSource Favourites; ItemClick ShowFavouriteCommand" 
       ... 
       android:choiceMode="singleChoice" 
       android:id="@+id/listFavourites" /> 
+0

Hallo @esiprogrammer danke für den Vorschlag. Ich habe die Zeile zu meiner Layout-Datei hinzugefügt, und leider hat sie das Problem nicht gelöst. :( – JDibble

+0

können Sie MVX Trace bereitstellen? Sie können 'IMvxTrace' implementieren, um herauszufinden, was schief läuft. [Hier geht's] (http://StackOverflow.com/questions/17233922/mvvmcross-mvx-trace-usage) – esiprogrammer

Verwandte Themen