Odd issue with MvvmCross, MvxListViewItem on Android

[亡魂溺海] 提交于 2020-01-05 10:36:39

问题


Have come across an issue using MvvmCross on Android.

I have a MvxListView that has been derived with a custom adapter so that I can show different views for various types of items. This works fine.

The issue I am seeing is that occasionally I have the following problem. The MvxListViewItem is created and shown on screen but none of the bindings have taken place (I'm using DelayBind as shown in all of the examples but the Action isn't invoked). If i then scroll this item offscreen and back again the DelayBind action is executed along with the bindings and the item is populated?

Very odd and has been causing me problems.

Has anyone else come across this and worked it out?

class BaseCustomListItemView: MvxListItemView
        {
            public BaseCustomListItemView(Context context, IMvxLayoutInflater layoutInflater, object dataContext, int templateId)
                : base(context, layoutInflater, dataContext, templateId)
            {

                this.DelayBind( () => 
                {
                    this.CreateBinding(this).For( "ProfileImage" ).To<ActivityItemVM>(vm => vm.ActivityUser).Apply();
                } );
            }

            public string ProfileImage
            {
                set 
                { 
                    var nameControl = this.FindViewById<PlayerNameControl>(Resource.Id.name);
                    var imageControl = this.FindViewById<PlayerProfileControl>(Resource.Id.photo);
                    imageControl.SetPlayerId( value, nameControl );

                }
            }

        }

My custom adapter looks like the following:

class CustomAdapter : MvxAdapter
        {
            public CustomAdapter(Context context)
                : base(context)
            {
            }

            protected override IMvxListItemView CreateBindableView(object dataContext, int templateId)
            {
                if ( dataContext is EventApprovedActivityItemVM )
                    return new EventApprovedActivityListItemView(base.Context, base.BindingContext.LayoutInflater, dataContext, templateId);

                if ( dataContext is AddedPlayerActivityItemVM )
                    return new AddedPlayerListItemView(base.Context, base.BindingContext.LayoutInflater, dataContext, templateId);

                if ( dataContext is AchievementLevelUpActivityItemVM )
                    return new AchievementLevelUpListItemView(base.Context, base.BindingContext.LayoutInflater, dataContext, templateId);

                if ( dataContext is EventCreatedActivityItemVM )
                {
                    var item = new EventCreatedActivityListItemView( base.Context, base.BindingContext.LayoutInflater, dataContext, templateId );
                    //                  item.ImageId = ( (EventCreatedActivityItemVM)dataContext ).ImageID;
                    return item;
                }

                return new BaseCustomListItemView(base.Context, base.BindingContext.LayoutInflater, dataContext, templateId);
            }

            public override int GetItemViewType(int position)
            {
                var item = GetRawItem(position);
                if (item is EventApprovedActivityItemVM)
                    return 0;
                if (item is AddedPlayerActivityItemVM)
                    return 1;
                if (item is AchievementLevelUpActivityItemVM)
                    return 2;
                if (item is EventCreatedActivityItemVM)
                    return 3;

                return 0;
            }

            public override int ViewTypeCount
            {
                get { return 4; }
            }

            protected override View GetBindableView(View convertView, object dataContext, int templateId)
            {
                if (dataContext is EventApprovedActivityItemVM)
                    templateId = Resource.Layout.eventapprovedactivityitem;

                if (dataContext is AddedPlayerActivityItemVM)
                    templateId = Resource.Layout.addedplayeractivityitem;

                if (dataContext is AchievementLevelUpActivityItemVM)
                    templateId = Resource.Layout.achievementlevelupactivityitem;

                if (dataContext is EventCreatedActivityItemVM)
                    templateId = Resource.Layout.eventcreatedactivityitem;

                return base.GetBindableView(convertView, dataContext, templateId);
            }

        }

Thanks Andy

来源:https://stackoverflow.com/questions/24355573/odd-issue-with-mvvmcross-mvxlistviewitem-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!