Xamarin.Android – NotifyMe Part III

Application in Xamarin.Android is done.

It is funny, I was developing the same application in Xamarin.Forms for the whole contest, and now I finished it in more difficult approach in one week. Of course I used whole prepared logic and view models, but it is a nice result anyway imho.
I ended previous post with TabLayout.

The final step was to copy view models code from previous application and prepare ListViews. I decided to use native ListView control with custom adapter instead of MvxListView with bindable ItemSource property so.. here is my friends fragment:

 

    public class FriendsFragment : MvxFragment<FriendsViewModel>
    {
        private ListView friendsListView;

        private FriendsAdapter friendsAdapter;

        private MvxPropertyChangedListener friendsListener;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var ignored = base.OnCreateView(inflater, container, savedInstanceState);
            this.EnsureBindingContextIsSet(savedInstanceState);
            var view = this.BindingInflate(Resource.Layout.FriendsFragmentView, container, false);

            friendsListView = view.FindViewById<ListView>(Resource.Id.friendsList);

            friendsAdapter = new FriendsAdapter(this.Activity, ViewModel);
            friendsListView.Adapter = friendsAdapter;

            friendsListener = new MvxPropertyChangedListener(ViewModel).Listen(() => ViewModel.Friends, (sender, e) =>
            {
               friendsAdapter.NotifyDataSetChanged();
            });

            friendsListView.ItemClick += (sender, e) =>
            {
                var selected = ViewModel.Friends[e.Position];
                ViewModel.NavigateToCreateMessageCommand.Execute(selected);
            };

            return view;
        }
    }

And custom adapter for friends list:

  public class FriendsAdapter : BaseAdapter
    {
        private FriendsViewModel viewModel;

        private Activity context;

        public FriendsAdapter(Activity context, FriendsViewModel vm)
        {
            this.viewModel = vm;
            this.context = context;
        }

        public override int Count
        {
            get
            {
                int result = 0;
                if (viewModel.Friends != null)
                {
                    result = viewModel.Friends.Count;
                }

                return result;
            }
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return null;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView;

            if (view == null)
            {
                view = context.LayoutInflater.Inflate(Resource.Layout.friendListItem, null);
            }

            if (viewModel.Friends != null && viewModel.Friends.Any())
            {
                var currentFriend = viewModel.Friends[position];

                var nameTextView = view.FindViewById<TextView>(Resource.Id.friendName);
                var imageView = view.FindViewById<ImageViewAsync>(Resource.Id.friendImage);
                nameTextView.Text = currentFriend.Name;

                ImageService.Instance.LoadUrl(currentFriend.ImageUrl)
                            .Transform(new CircleTransformation())
                            .Into(imageView);
            }

            return view;
        }

As you can see images in ListView are loading from url. It can be cause of performance problems. The solution is caching and asynchronous loading.

In Xamarines #1 meetup Daniel Luberda presented awesome FFImageLoading library so I decided to use it. My usage was very simple but I can really recommend you this library.
It takes care about caching and loading images for us. It also allows to use image transformations. Thanks to it, my list looks great with circular images 🙂

Complete sending process:

Everything works fine. Mr.Bean received notification on his device.
Xamarin.iOS turn.
Let’s do this.

Leave a Reply

Your email address will not be published. Required fields are marked *