protected override void OnItemCountChangeRequested(ChatExample adapter, int count) { base.OnItemCountChangeRequested(adapter, count); // Generating some random models var newModels = new ChatMessageModel[count]; for (int i = 0; i < count; ++i) { newModels[i] = CreateRandomModel(i, i != 1); // the second model will always have an image, for demo purposes } adapter.Data.ResetItems(newModels, true); }
/// <inheritdoc/> protected override void UpdateViewsHolder(ChatMessageViewsHolder newOrRecycled) { // Initialize the views from the associated model ChatMessageModel model = Data[newOrRecycled.ItemIndex]; newOrRecycled.UpdateFromModel(model, _Params); if (model.HasPendingVisualSizeChange) { // Height will be available before the next 'twin' pass, inside OnItemHeightChangedPreTwinPass() callback (see above) newOrRecycled.MarkForRebuild(); // will enable the content size fitter //newOrRecycled.contentSizeFitter.enabled = true; ScheduleComputeVisibilityTwinPass(true); } if (!newOrRecycled.IsPopupAnimationActive && newOrRecycled.itemIndexInView == GetItemsCount() - 1) // only animating the last one { newOrRecycled.ActivatePopulAnimation(Time); } }
/// <summary>Utility getting rid of the need of manually writing assignments</summary> public void UpdateFromModel(ChatMessageModel model, MyParams parameters) { timeText.text = model.TimestampAsDateTime.ToString("HH:mm"); string messageText = "[#" + ItemIndex + "] " + model.Text; if (text.text != messageText) { text.text = messageText; } leftIcon.gameObject.SetActive(!model.IsMine); rightIcon.gameObject.SetActive(model.IsMine); if (model.ImageIndex < 0) { image.gameObject.SetActive(false); } else { image.gameObject.SetActive(true); image.sprite = parameters.availableChatImages[model.ImageIndex]; } if (model.IsMine) { messageContentPanelImage.rectTransform.pivot = new Vector2(1.4f, .5f); messageContentPanelImage.color = new Color(.75f, 1f, 1f, colorAtInit.a); _RootLayoutGroup.childAlignment = _MessageContentLayoutGroup.childAlignment = text.alignment = TextAnchor.MiddleRight; _RootLayoutGroup.padding.right = paddingAtIconSide; _RootLayoutGroup.padding.left = paddingAtOtherSide; } else { messageContentPanelImage.rectTransform.pivot = new Vector2(-.4f, .5f); messageContentPanelImage.color = colorAtInit; _RootLayoutGroup.childAlignment = _MessageContentLayoutGroup.childAlignment = text.alignment = TextAnchor.MiddleLeft; _RootLayoutGroup.padding.right = paddingAtOtherSide; _RootLayoutGroup.padding.left = paddingAtIconSide; } }