- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如何在 MvxListView
中保持项目突出显示,直到它被取消选中或直到另一个项目被选中?
我的程序有一个 MvxListView
可以正确显示项目列表。用户可以通过单击来选择一个项目,然后单击保存按钮。所选项目存储在 MyChosenItem
中,直到保存按钮代码需要它为止。目前,选中的项目在返回未选中的颜色之前会在一瞬间保持高亮。
MvxListView
是这样创建的:
<Mvx.MvxListView
android:layout_width="match_parent"
android:layout_height="260dp"
android:layout_marginTop="40dp"
android:id="@+id/MyMvxListViewControl"
local:MvxBind="ItemsSource MyItems; SelectedItem MyChosenItem"
local:MvxItemTemplate="@layout/my_item_layout" />
这是Layout/my_item_layout.xaml
:
<?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/Project.Ui.Droid"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="300.0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="20dp"
android:textColor="#000000"
local:MvxBind="Text Field1" />
<TextView
android:layout_width="250.0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="20dp"
android:textColor="#000000"
local:MvxBind="Text Field2" />
</LinearLayout>
最佳答案
此方法提供了一种自定义哪些项目保持突出显示的简便方法。我决定这样做是因为它让我可以完全控制突出显示的内容以及它在列表中的显示方式。 (此示例仅突出显示一项,但可以轻松扩展以突出显示更多项。)
在原始问题中,MvxListView 链接到关联 View 模型中的 MyItems
和 MyChosenItem
。 MyItems
是 Item
的集合,MyChosenItem
只是一个 Item
。我将 isItemSelected
添加到 Item
。 Item
类现在如下所示:
public class Item : MvxViewModel
{
private string _field1;
private string _field2;
private bool _isItemSelected = false;
public string Field1
{
get { return _field1; }
set
{
_field1= value;
RaisePropertyChanged("Field1");
}
}
public string Field2
{
get { return _field2; }
set
{
_field2= value;
RaisePropertyChanged("Field2");
}
}
public bool isItemSelected
{
get { return _isItemSelected; }
set
{
_isItemSelected = value;
RaisePropertyChanged("isItemSelected");
}
}
}
注意:Item
类扩展了 MvxViewModel
,因此可以调用 RaisePropertyChange()
。这允许 my_item_layout.xaml
在该属性更改时收到通知。
从 MvxListView 的 SelectedItem
绑定(bind)到的属性更新 isItemSelected
的每个实例。在本例中,它是关联 View 模型中的 MyChosenItem
属性。这是新代码的样子:
public Item MyChosenItem
{
get { return _myChosenItem; }
set
{
if (_myChosenItem != value)
{
_myChosenItem = value;
UpdateItemSelections();
RaisePropertyChanged("MyChosenItem");
}
}
}
// Select MyChosenItem and unselect all other items
private void UpdateItemSelections()
{
if( MyItems.Count > 0)
{
for (int index = 0; index < MyItems.Count; index++)
{
// If the chosen item is the same, mark it as selected
if (MyItems[index].Field1.Equals(MyChosenItem.Field1)
&& MyItems[index].Field2.Equals(MyChosenItem.Field2))
{
MyItems[index].isItemSelected = true;
}
else
{
// Only trigger the property changed event if it needs to change
if (MyItems[index].isItemSelected)
{
MyItems[index].isItemSelected = false;
}
}
}
}
}
将 UpdateItemSelections()
修改为您想要的任何选择行为将非常容易。
让每一行根据 isItemSelected
属性做一些事情。我只是通过控制 View 的可见性属性来改变背景颜色。然而,一切皆有可能。 isItemSelected
甚至可以传递给自定义控件以获得一些非常有趣的视觉效果。我的新 Layout/my_item_layout.xaml
如下所示:
<?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/Project.Ui.Droid"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- SELECTED BACKGROUND COLOR -->
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
local:MvxBind="Visibility isItemSelected,Converter=BoolToViewStates" />
<TextView
android:layout_width="300.0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="20dp"
android:textColor="#000000"
local:MvxBind="Text Field1" />
<TextView
android:layout_width="250.0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="20dp"
android:textColor="#000000"
local:MvxBind="Text Field2" />
</LinearLayout>
编辑
在设置 SelectedItem
时,最好使用 MvxCommand
而不是触发突出显示的操作。似乎 SelectedItem
只有在尚未被选中时才会被设置。点击一个项目将选中它。点击另一个项目将改变选择。再次点击同一项目不会取消选择。这意味着一旦选择了一项,就必须保持选中一项。如果您需要能够取消选择列表中的所有项目,请按照对原始说明的这些修改进行操作:
将 MvxCommand
添加到 View 模型。从 MvxCommand
而不是从 MyChosenItem
调用 UpdateItemSelections()
。
public MvxCommand ItemSelectedCommand { get; private set; }
// Constructor
public ItemSelectionViewModel()
{
ItemSelectedCommand = new MvxCommand(OnItemSelected);
}
public Item MyChosenItem
{
get { return _myChosenItem; }
set
{
if (_myChosenItem != value)
{
_myChosenItem = value;
//UpdateItemSelections(); // Move this to OnItemSelected()
RaisePropertyChanged("MyChosenItem");
}
}
}
private void OnItemSelected()
{
UpdateItemSelections();
}
更改 UpdateItemSelections()
以切换 isItemSelected
属性,而不是始终将其设置为 true:
// Select MyChosenItem and unselect all other items
private void UpdateItemSelections()
{
if( MyItems.Count > 0)
{
for (int index = 0; index < MyItems.Count; index++)
{
// If the chosen item is the same, mark it as selected
if (MyItems[index].Field1.Equals(MyChosenItem.Field1)
&& MyItems[index].Field2.Equals(MyChosenItem.Field2))
{
// Toggle selected status
MyItems[index].isItemSelected = !MyItems[index].isItemSelected;
}
else
{
// Only trigger the property changed event if it needs to change
if (MyItems[index].isItemSelected)
{
MyItems[index].isItemSelected = false;
}
}
}
}
}
在保存或对列表中的选定项目执行任何操作时,请记住检查 MyChosenItem.isItemSelected == true
。 MyChosenItem
中可能有一个值在用户看到的 ListView 中未被选中。
在 MvxListView
的布局定义中将 MvxCommand
绑定(bind)到 ItemClick
:
<Mvx.MvxListView
android:layout_width="match_parent"
android:layout_height="260dp"
android:layout_marginTop="40dp"
android:id="@+id/MyMvxListViewControl"
local:MvxBind="ItemsSource MyItems; SelectedItem MyChosenItem; ItemClick ItemSelectedCommand"
local:MvxItemTemplate="@layout/my_item_layout" />
关于android - 如何突出显示 MvxListView 中的选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18364907/
在下面的示例中,我想将 ItemClick 命令绑定(bind)到 MvxListView 中的项目。 在这里,我的 ViewModel 中有一个包含狗列表的人员列表。 ItemsSource Has
在我的 Android 应用程序中,我创建了一个垂直布局,以将图像(缩略图)的水平列表置于列表中所选图像的较大 View 之上。出于某种原因,我无法让水平列表水平显示,它只显示为垂直列表。
我想让 CustomChoiceList 与 MvvmCross 一起工作,但很难让示例正常工作,无法选择 ListItem。 事实上,该示例使用了一个自定义的 LinearLayout,它扩展了 L
我正在使用 mvvmcross(非常有趣),但在从 mvxlistview 添加和删除项目时一直遇到问题: 我的 View 绑定(bind)到从 Web 服务器检索的项目列表,因此它在不同的线程中完成
我正在使用绑定(bind)到 observablecollection 的 MvxListView 来显示聊天消息。我找到了一种方法, ListView 始终使用以下 2 个属性向下滚动到底部: an
当我从列表中删除一个项目时,MvxListView 不会刷新。 我的 list (没什么花哨的): private List items = new List(); public List Items
我有一个 MvxListView,我通过单击“添加”按钮来填充它,它运行良好。问题是当我添加项目时 ListView 的高度需要扩展。我试过设置android:layout_height="wrap_
我正在尝试在 Android 上实现类似于 iOS 的分组 ListView 。因此,我正在尝试编写自己的支持分组节标题的自定义 MvxAdapter。从 axml 构造的默认 MvxListView
如何在 MvxListView 中保持项目突出显示,直到它被取消选中或直到另一个项目被选中? 我的程序有一个 MvxListView 可以正确显示项目列表。用户可以通过单击来选择一个项目,然后单击保存
假设我有一个带有 MvxListView 的简单布局: 我的项目模板布局如下: 我想从代码隐藏中为我的 itemtemplate 中的两个 textview
是否可以嵌套绑定(bind) MvxListView(mvvmcross android v3.1 系列)? 我发现嵌套绑定(bind)失败: MvxBind:Warning: 1.17 Unabl
我正在使用一个包含 MvxSpinner 的 MvxListView。当我的应用程序运行时,跟踪显示以下几个实例: 当前微调器 SelectedItem 绑定(bind)中不允许使用空值 我知道数据对
我正在使用 Mvvmcross 在 Xamarin 中构建一个 Android 应用程序。在这个应用程序中,我需要一个 F.a.q.页面,为此我需要一个 ListView 。使用 MvxListVie
我的 MvxListView 及其绑定(bind)有问题。也就是说,如果我不添加项目模板,它绑定(bind)得很好,但在项目模板中,绑定(bind)数据不再显示。 HelpView.axml
以下布局(SearchLayoutView)中的 MvxListView 不像正常的 xamarin ListView 那样流畅。当我向下滚动列表时,项目卡住了。在这里,我将模板 (search_li
我目前正在为 Android 和 iOS 实现跨平台应用。 因为我想使用 MvvmCross 和弹出式导航,如果发现 Benjamin Hysell 的演示(使用 DrawerLayout)非常有用:
在 MvvmCross 中是否可以有一个顶部带有 Search EditText 的 Android MvxListView?以及如何? 最佳答案 在您的 View.axml 中:
如果我有一个具有以下 MvxListView 定义的 View : 不是将 MvxItemTemplate 硬编码到 itemtemplate1,是否可以根据我想在此 View 中显示的数据类型动态
我尝试添加一个带有 MvxListView 和绑定(bind)的 fragment 。我正在使用 v4.0.0.0 beta2。 这是布局: fragment : public class Acc
所以我有一张幻灯片列表: SlideListView.axml: SlideListItemView.axml: 所以每张幻灯片都有一个文本和删除按钮。 SlideListIte
我是一名优秀的程序员,十分优秀!