public override void OnBindViewHolder(RecyclerView.ViewHolder vh, int position) { try { int type = GetItemViewType(position); var item = MessageList[type]; if (item == null) { return; } switch (item.ApiType) { case ApiType.Text: { TextViewHolder holder = vh as TextViewHolder; LoadTextOfChatItem(holder, position, item); break; } case ApiType.Image: { ImageViewHolder holder = vh as ImageViewHolder; LoadImageOfChatItem(holder, position, item); break; } } } catch (Exception exception) { Console.WriteLine(exception); } }
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) { try { //Setup your layout here >> var itemView = MessageList[viewType]; if (itemView != null) { if (itemView.ApiPosition == ApiPosition.Right && itemView.ApiType == ApiType.Text) { View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.Right_MS_view, parent, false); TextViewHolder textViewHolder = new TextViewHolder(row, ActivityContext); return(textViewHolder); } if (itemView.ApiPosition == ApiPosition.Left && itemView.ApiType == ApiType.Text) { View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.Left_MS_view, parent, false); TextViewHolder textViewHolder = new TextViewHolder(row, ActivityContext); return(textViewHolder); } if (itemView.ApiPosition == ApiPosition.Right && itemView.ApiType == ApiType.Image) { View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.Right_MS_image, parent, false); ImageViewHolder imageViewHolder = new ImageViewHolder(row); return(imageViewHolder); } if (itemView.ApiPosition == ApiPosition.Left && itemView.ApiType == ApiType.Image) { View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.Left_MS_image, parent, false); ImageViewHolder imageViewHolder = new ImageViewHolder(row); return(imageViewHolder); } return(null); } return(null); } catch (Exception exception) { Console.WriteLine(exception); return(null); } }
private void LoadImageOfChatItem(ImageViewHolder holder, int position, ChatMessagesDataObject message) { try { Console.WriteLine(position); string imageUrl = message.Image.Contains("upload/chat/") && !message.Image.Contains(Client.WebsiteUrl) ? Client.WebsiteUrl + "/" + message.Image : message.Image; string fileSavedPath; holder.Time.Text = Methods.Time.TimeAgo(Convert.ToInt32(message.Time)); if (imageUrl.Contains("http://") || imageUrl.Contains("https://")) { var fileName = imageUrl.Split('/').Last(); string imageFile = Methods.MultiMedia.GetMediaFrom_Gallery(Methods.Path.FolderDcimImage, fileName); if (imageFile == "File Dont Exists") { holder.LoadingProgressView.Indeterminate = false; holder.LoadingProgressView.Visibility = ViewStates.Visible; string filePath = Path.Combine(Methods.Path.FolderDcimImage); string mediaFile = filePath + "/" + fileName; fileSavedPath = mediaFile; WebClient webClient = new WebClient(); webClient.DownloadDataAsync(new System.Uri(imageUrl)); //webClient.DownloadProgressChanged += (sender, args) => //{ // var progress = args.ProgressPercentage; //}; webClient.DownloadDataCompleted += (s, e) => { try { if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } File.WriteAllBytes(mediaFile, e.Result); //var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); //mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(mediaFile))); //ActivityContext.SendBroadcast(mediaScanIntent); // Tell the media scanner about the new file so that it is // immediately available to the user. MediaScannerConnection.ScanFile(Application.Context, new[] { mediaFile }, null, null); var file = Uri.FromFile(new Java.IO.File(mediaFile)); Glide.With(ActivityContext).Load(file.Path).Apply(Options).Into(holder.ImageView); holder.LoadingProgressView.Indeterminate = false; holder.LoadingProgressView.Visibility = ViewStates.Gone; } catch (Exception ex) { Console.WriteLine(ex); } }; } else { fileSavedPath = imageFile; var file = Uri.FromFile(new Java.IO.File(imageFile)); Glide.With(ActivityContext).Load(file.Path).Apply(Options).Into(holder.ImageView); holder.LoadingProgressView.Indeterminate = false; holder.LoadingProgressView.Visibility = ViewStates.Gone; } } else { fileSavedPath = imageUrl; var file = Uri.FromFile(new Java.IO.File(imageUrl)); Glide.With(ActivityContext).Load(file.Path).Apply(Options).Into(holder.ImageView); //GlideImageLoader.LoadImage(ActivityContext, imageUrl, holder.ImageView, ImageStyle.CenterCrop, ImagePlaceholders.Drawable); holder.LoadingProgressView.Indeterminate = false; holder.LoadingProgressView.Visibility = ViewStates.Gone; } if (!holder.ImageView.HasOnClickListeners) { holder.ImageView.Click += (sender, args) => { try { string imageFile = Methods.MultiMedia.CheckFileIfExits(fileSavedPath); if (imageFile != "File Dont Exists") { Java.IO.File file2 = new Java.IO.File(fileSavedPath); var photoUri = FileProvider.GetUriForFile(ActivityContext, ActivityContext.PackageName + ".fileprovider", file2); Intent intent = new Intent(); intent.SetAction(Intent.ActionView); intent.AddFlags(ActivityFlags.GrantReadUriPermission); intent.SetDataAndType(photoUri, "image/*"); ActivityContext.StartActivity(intent); } else { Intent intent = new Intent(Intent.ActionView, Uri.Parse(imageUrl)); ActivityContext.StartActivity(intent); } } catch (Exception e) { Console.WriteLine(e); } }; } } catch (Exception e) { Console.WriteLine(e); } }