【问题标题】:MvxGridView ItemClick not FiringMvxGridView ItemClick 未触发
【发布时间】:2019-12-09 20:41:49
【问题描述】:

我正在努力做一些看似微不足道的事情。

我有一个像这样的 MvxGridView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <Mvx.MvxGridView
    android:id="@+id/subjects_gv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    android:numColumns="2"
    android:verticalSpacing="15dp"
    android:gravity="center"
    app:MvxItemTemplate="@layout/assigned_subject_view"
    app:MvxBind="ItemClick ShowSubjectFeatures;ItemsSource Subjects;"/>
</LinearLayout>

这是模板

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:local="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:clickable="true"
  local:cardUseCompatPadding="true"
  local:cardCornerRadius="5dp">

  <RelativeLayout
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground">
    <ImageView
      android:layout_centerInParent="true"
      android:id="@+id/iv_subject_image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/english" />
    <TextView
      android:layout_centerInParent="true"
      android:layout_alignParentBottom="true"
      android:id="@+id/tv_subject_Name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAllCaps="true"
      android:gravity="center"
      android:maxLines="2"
      android:stretchMode="spacingWidthUniform"
      android:textSize="@dimen/very_small_text"
      local:MvxBind="Text Name" />
  </RelativeLayout>

</android.support.v7.widget.CardView>

这是视图模型

private readonly IMvxNavigationService _navigationService;
        private readonly ISubjectsService _subjectsService;

        public SubjectsViewModel(IMvxNavigationService navigationService, ISubjectsService subjectsService)
        {
            _navigationService = navigationService;
            _subjectsService = subjectsService;
        }

        public MvxObservableCollection<SubjectModel> Subjects { get; set; } = new MvxObservableCollection<SubjectModel>();

        public MvxCommand<SubjectModel> ShowSubjectFeatures
        {
            get
            {
                // Navigate to subject details viewmodel
                return new MvxCommand<SubjectModel>(async subject => await NavigateToFeatures(subject));
            }
        }

        public override async Task Initialize()
        {
            var response = await _subjectsService.GetAssignedSubjects();
            Subjects.AddRange(response.Subjects);
        }

        private async Task NavigateToFeatures(SubjectModel model)
        {
            await _navigationService.Navigate<SubjectFeatureViewModel, int>(model.Id);
        }

问题是当我点击一个网格项目时,该命令没有被触发。 其他信息,gridview 显示在一个片段中,SubjectFeaureView 也是一个片段,因此它将从一个片段导航到另一个片段。我不认为这应该是一个问题,寿。我错过了什么?我对此很陌生。

【问题讨论】:

  • 为什么你的 CardView 上有android:clickable="true"?
  • @Cheesebaron 因为我希望它可以点击,或者默认情况下不可点击。刚刚注意到我在 RelativeLayout 上也有它,但在那里不需要。
  • 如果点击命令没有被触发,那是因为某些东西吸收了点击事件并且它没有传递给 GridView 的适配器。
  • 好点,只是不知道有什么东西可以吸收它。
  • 使用 Android Studio 中的 Layout Inspector 转储视图层次结构并检查您的布局

标签: c# android xamarin xamarin.android mvvmcross


【解决方案1】:

删除 android:clickable="true" 解决了我的问题。

【讨论】:

  • 恭喜,您能否将您的回复标记为答案,以便其他有类似问题的人可以从这里获得帮助?谢谢 。 :)
猜你喜欢
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多