-
Notifications
You must be signed in to change notification settings - Fork 483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
当totalCount数量减少时(比如从50个->10个),调用Refresh会导致item都看不见了,是否考虑这种情况自动刷新到底部 #186
Comments
这个问题其实和#185 相关,Refresh只用于明确知道只需要刷新数据、不修改布局的情况(这样相比Refill节约性能) /// <summary>
/// Refresh item data
/// </summary>
public void RefreshCells() |
嗯,那我考虑在业务层处理一下数量的判断,当数量减少且有部分留白时(这块有点复杂T_T),让列表”沉底“ |
我也写过类似的业务代码orz:当个数不够的时候,转而调用 这块我也是纠结过库应该怎么处理比较好,毕竟遇到不同项目的需求其实是不太一样的... 所以我目前的设计思路是暴露相对底层的API,但业务代码就要多写几行了。。。 |
解决:统一问题+解决数量少了的问题 void RefillCellsInternal(int dataCount = -1, int startItem = 0)
{
bool isEndEmpty = false;
var lastCount = LoopScrollRect.totalCount;
var changeCount = dataCount - LoopScrollRect.totalCount;
if (dataCount >= 0)
LoopScrollRect.totalCount = dataCount;
//指定到某一位置,Refill
if (startItem >= 0)
{
LoopScrollRect.RefillCells(startItem);
}
//第一次刷新(或者上一次数量为0),必须Refill
else if (lastCount == 0)
{
LoopScrollRect.RefillCells();
}
//数量不变时或者增加时,Refresh
else if(changeCount >= 0)
{
LoopScrollRect.RefreshCells();
}
//数量减少时
else
{
var lastIdx = LoopScrollRect.GetLastItem(out _);
if (lastIdx > LoopScrollRect.totalCount - 1)
isEndEmpty = true;
//底部留白,从底部重新填充
if (isEndEmpty)
LoopScrollRect.RefillCellsFromEnd();
//底部不留白,保持当前滚动位置
else
LoopScrollRect.RefreshCells();
}
} |
又发现了一出细节问题,即 public void RefillCellsFromEnd(int endItem = 0, bool alignStart = false)
{
if (!Application.isPlaying)
return;
itemTypeEnd = reverseDirection ? endItem : totalCount - endItem;
itemTypeStart = itemTypeEnd;
if (totalCount >= 0 && itemTypeStart % contentConstraintCount != 0)
{
itemTypeStart = (itemTypeStart / contentConstraintCount) * contentConstraintCount;
}
ReturnToTempPool(!reverseDirection, m_Content.childCount);
float sizeToFill = GetAbsDimension(viewRect.rect.size), sizeFilled = 0;
bool first = true;
// issue 169: fill last line
if (itemTypeStart < itemTypeEnd)
{
itemTypeEnd = itemTypeStart;
float size = reverseDirection ? NewItemAtStart(!first) : NewItemAtEnd(!first);
if (size >= 0)
{
first = false;
sizeFilled += size;
}
}
while (sizeToFill > sizeFilled)
{
float size = reverseDirection ? NewItemAtEnd(!first) : NewItemAtStart(!first);
if (size < 0)
break;
first = false;
sizeFilled += size;
}
// refill from start in case not full yet
while (sizeToFill > sizeFilled)
{
float size = reverseDirection ? NewItemAtStart(!first) : NewItemAtEnd(!first);
if (size < 0)
break;
first = false;
sizeFilled += size;
}
Vector2 pos = m_Content.anchoredPosition;
float dist = alignStart ? 0 : Mathf.Max(0, sizeFilled - sizeToFill);
if (reverseDirection)
dist = -dist;
if (direction == LoopScrollRectDirection.Vertical)
pos.y = dist;
else
pos.x = -dist;
m_Content.anchoredPosition = pos;
m_ContentStartPosition = pos;
ClearTempPool();
// force build bounds here so scrollbar can access newest bounds
LayoutRebuilder.ForceRebuildLayoutImmediate(m_Content);
Canvas.ForceUpdateCanvases();
UpdateBounds(false);
UpdateScrollbars(Vector2.zero);
StopMovement();
UpdatePrevData();
//下面补丁代码
if (direction == LoopScrollRectDirection.Horizontal)
horizontalNormalizedPosition = 1;
else
verticalNormalizedPosition = 1;
} |
这个需求我理解,我琢磨了下准备先多扩展下当前的demo scene,把常见的一些模式都作为demo先提供一份看看反馈~ 等这部分稳定了之后、挪入LoopScrollRect中(主要是加进来的API我想尽可能保持稳定,这样用户升级的时候最好不要遇到breaking change)
和这个函数比较像:先检查
我怀疑是因为本身元素比较少导致无法填充满了?我本地测了下这种情况: |
master提交了08744f8 ,处理有padding情况下的 sample代码我尽快整理好之后提交 |
这个有看到,其实我参考你的代码试了下, |
哈哈,我的代码很丑陋,为了实现需求硬拼的:
期待你的优雅实现 😄 |
20250207162709_rec_.mp4
The text was updated successfully, but these errors were encountered: