2025-03-12 20:59:12 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2025-04-01 15:21:02 +08:00
|
|
|
using AlicizaX.ObjectPool;
|
2025-03-12 20:59:12 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace AlicizaX.UI.RecyclerView
|
|
|
|
{
|
|
|
|
public class MixedViewProvider : ViewProvider
|
|
|
|
{
|
|
|
|
[SerializeField] private ViewHolder chatLeftViewHolder;
|
|
|
|
[SerializeField] private ViewHolder chatRightViewHolder;
|
|
|
|
|
|
|
|
private Dictionary<string, ViewHolder> dict = new();
|
|
|
|
|
|
|
|
public MixedViewProvider(RecyclerView recyclerView, ViewHolder[] templates) : base(recyclerView, templates)
|
|
|
|
{
|
|
|
|
foreach (var template in templates)
|
|
|
|
{
|
|
|
|
dict[template.name] = template;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override ViewHolder GetTemplate(string viewName)
|
|
|
|
{
|
|
|
|
if (templates == null || templates.Length == 0)
|
|
|
|
{
|
|
|
|
throw new NullReferenceException("ViewProvider templates can not null or empty.");
|
|
|
|
}
|
2025-04-01 15:21:02 +08:00
|
|
|
|
2025-03-12 20:59:12 +08:00
|
|
|
return dict[viewName];
|
|
|
|
}
|
|
|
|
|
|
|
|
public override ViewHolder[] GetTemplates()
|
|
|
|
{
|
|
|
|
if (templates == null || templates.Length == 0)
|
|
|
|
{
|
|
|
|
throw new NullReferenceException("ViewProvider templates can not null or empty.");
|
|
|
|
}
|
2025-04-01 15:21:02 +08:00
|
|
|
|
2025-03-12 20:59:12 +08:00
|
|
|
return dict.Values.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override ViewHolder Allocate(string viewName)
|
|
|
|
{
|
2025-04-01 15:21:02 +08:00
|
|
|
return ViewHolderObjectPool.Allocate(GetTemplate(viewName), recyclerView.Content);
|
2025-03-12 20:59:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Free(string viewName, ViewHolder viewHolder)
|
|
|
|
{
|
2025-04-01 15:21:02 +08:00
|
|
|
ViewHolderObjectPool.Free(viewHolder);
|
2025-03-12 20:59:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Reset()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|