【问题标题】:How can I create a scroll list that has not always the same amount of rows?如何创建一个行数并不总是相同的滚动列表?
【发布时间】:2019-05-22 20:37:01
【问题描述】:

我的滚动列表的行数并不总是相同,当最后一行在滚动列表底部完全可见时,我想禁用向下滚动。

但是现在,如果最后一行(在本例中为第 13 行)在列表底部完全可见,则仍然可以向下滚动。然后它看起来像第三张图片(更多滚动后),但我希望它看起来像第二张图片(滚动后)。

我该怎么做才能在最后一行完全可见后无法继续向下滚动?

更新:现在可以使用了。此代码版本使用可变滚动速度。

我的代码:

//Creating the scroll list: 

public List<Vector2> ScrolllistInitialRowPosYList = new List<Vector2>();
public List<Vector2> ScrolllistRowPosYList = new List<Vector2>();
Microsoft.Xna.Framework.Rectangle ScrollPointRectangle, tappRect, ScrollRectangle;
int ScrollLimitTopPosY = 200, ScrollLimitBottomPosY = 400, RowHeight = 60, ScrollPointStartPosY;
float ScrollAreaHeight, HeightallRows, ScrollSpeed;

ScrollPointStartPosY = ScrollLimitTopPosY;
ScrollPointRectangle = new Microsoft.Xna.Framework.Rectangle(500, ScrollPointStartPosY, ScrollPointSprite.Width, ScrollPointprite.Height);
tappRect = new Microsoft.Xna.Framework.Rectangle(-150, -100, 10, 10);
ScrollRectangle = new Microsoft.Xna.Framework.Rectangle(500, 300, ScrollRectangleSprite.Width, ScrollRectangleSprite.Height);

int FirstRowPosY = 0;
int NumberofRows = 13;
ScrollAreaHeight = ScrollLimitBottomPosY - ScrollLimitTopPosY;
int ListViewportHeight = 6 * RowHeight;
HeightallRows = NumberofRows * RowHeight - ListViewportHeight;

ScrollSpeed = HeightallRows / ScrollAreaHeight;

for (int i = 0; i <= NumberofRows - 1; i++)
{
     ScrolllistInitialRowPosYList.Add(new Vector2(500, FirstRowPosY));
     ScrolllistRowPosYList.Add(new Vector2(500, FirstRowPosY));                     
     FirstRowPosY += RowHeight;
}

//Updating gestures:

                    case GestureType.FreeDrag:
                    if (tappRect.Intersects(ScrollRectangle))
                    {
                        tappRect = new Microsoft.Xna.Framework.Rectangle((int)gs.Position.X, (int)gs.Position.Y, 10, 10);

                            if (gs.Delta.Y > 0)
                            {
                             if (ScrollPointRectangle.Y >= ScrollLimitTopPosY + 2)
                                ScrollPointRectangle = new Microsoft.Xna.Framework.Rectangle(500, ScrollPointRectangle.Y - 2, ScrollPointSprite.Width, ScrollPointSprite.Height);
                          else
                                ScrollPointRectangle = new Microsoft.Xna.Framework.Rectangle(500, ScrollLimitTopPosY, ScrollPointSprite.Width, ScrollPointSprite.Height);
                            }
                            else
                            {
                                if (gs.Delta.Y < 0)
                                {
                                    if (ScrollPointRectangle.Y <= ScrollLimitBottomPosY - 2)
                                      ScrollPointRectangle = new Microsoft.Xna.Framework.Rectangle(500, ScrollPointRectangle.Y + 2, ScrollPointSprite.Width, ScrollPointSprite.Height);
                                    else
                                      ScrollPointRectangle = new Microsoft.Xna.Framework.Rectangle(500, ScrollLimitBottomPosY, ScrollPointSprite.Width, ScrollPointSprite.Height);
                                }
                            }
                    }
                    break;

//Updating the scroll list after someone srolled up or down:

        for (int i = 0; i <= ScrolllistRowPosYList.Count - 1; i++)
        {
            int ScrollPosYCurrent = ScrollPointRectangle.Y;

            int ScrollPosYDifference = ScrollPosYCurrent - ScrollPointStartPosY;
            float RowPosYDifference = ScrollSpeed * ScrollPosYDifference;

            int RowInitialPosY = (int)ScrolllistInitialRowPosYList[i].Y;

            float RowCurrentPosY = RowInitialPosY - RowPosYDifference;

            ScrolllistRowPosYList[i] = new Vector2(ScrolllistRowPosYList[i].X, RowCurrentPosY);
        }

【问题讨论】:

  • 您可以计算显示所有行所需的总高度,然后减去列表框的 viewport 高度。该计算的结果是范围从 0 到 的滚动条的最大值,可用于钳制项目,以便列表项无法滚动到可滚动区域的底部。
  • 感谢@Bradley Uffner。现在可以了。我更新了我的代码。我添加了 ListViewportHeight 并将一些 int 变量更改为 float 变量。
  • 我还是有滚动速度的问题,当我只有几行时滚动太慢,例如15。另一方面,当我有很多行时滚动太快,因为示例 200. 如何调整滚动速度,使其在一种特殊情况下不会太慢或太快?
  • 我不会让滚动速度取决于项目与高度的比率,而是考虑让它以恒定速度滚动。只需弄清楚每秒舒适的像素数是多少,然后将该数字除以您的帧速率,然后按您想要滚动的每一帧的像素数滚动。这是可滚动控件在 Windows 中工作的基本方式。我会遵循那个熟悉的模式。
  • 谢谢。匀速滚动解决了这个问题。

标签: c# xna


【解决方案1】:
  1. 你不能画它们。
  2. 您可以有 2 个List&lt;row&gt; 变量,其中一个将存储所有行,另一个仅存储应查看和交互的行。

编辑:我看错了。忘记我写的第一点。 使用第二点中的两个列表。滚动时,检查最后一个可见行是否是“所有行”列表中的最后一行。如果是这样,请不要滚动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多