我正在使用MvvmCross开发一个应用程序,我在android应用程序中遇到问题,设置绑定以捕获组成MvxListView项目的两个不同控件中的单击操作。 ViewModel中所有数据的绑定都可以正常工作 这是我的FavouritesView的AXML
<?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>
MvxItemTemplate就在这里:
<?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>
这是我的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"); } }
}
}
我的问题的症状是,当&#34;(x)删除&#34;时,View会收到DeleteFavouriteMessage。单击按钮。 单击该项时,不会从MvxListView触发ItemClick事件ShowFavouriteCommand。
我确信我正在做某事,或者遗漏了一些简单的事情,但我正在努力取得任何进展。
答案 0 :(得分:3)
我之前遇到过这个。 MvxItemTemplate
布局中的按钮正在窃取焦点。尝试为布局中的按钮设置android:focusable="false"
。
Changing focus from Button in ListView row to list item
我正在更新此答案以使其更加彻底。
在android:focusable="false"
项目中为常用Button控件设置ListView
可解决此问题。如果您使用的是ImageButton
,则无效。
对于ImageButton
,您需要在布局的根视图上设置android:descendantFocusability="blocksDescendants"
。
请在此处查看已接受的答案:can't click on listview row with imagebutton
答案 1 :(得分:0)
我认为你遗失了android:choiceMode="singleChoice"
<Mvx.MvxListView
local:MvxBind="ItemsSource Favourites; ItemClick ShowFavouriteCommand"
...
android:choiceMode="singleChoice"
android:id="@+id/listFavourites" />