《List》之循环遍历 ----轮播图取数据

从 List 中获取指定步长的元素,循环获取,当剩余数量不足步长时,从头开始查询遍历。不作赘述,直接展示:

public Button btn;

    /// <summary>
    /// 所有数据
    /// </summary>
    private List<int> totalList;
    
    /// <summary>
    /// 获取数据条数
    /// </summary>
    public int step;

    /// <summary>
    /// 当前页数:不需要翻页处理的,可以忽略
    /// </summary>
    private int PageIndex = 1;

    /// <summary>
    /// 起始位置
    /// </summary>
    private int startIndex = 0;

    /// <summary>
    /// Log打印使用,可以忽略
    /// </summary>
    private int count = 0;

    /// <summary>
    /// 是否超出过上限
    /// </summary>
    private bool isLimit = false;

    void Start()
    {
        btn.onClick.AddListener(OnGetResult);

        totalList = new List<int>();
        for (int i = 0; i < 7; i++)
        {
            totalList.Add(i);
        }
        // 初始化获取条数
        step = 5;
    }
    
    void OnGetResult()
    {
        count += 1;
        var result = GetList(totalList, step);
        PageIndex++;
        startIndex++;
        Debug.LogError(string.Format("第[{0}]次  {1}, {2}, {3}, {4}, {5}", count, result[0], result[1], result[2], result[3], result[4]));
    }
    List<int> GetList(List<int> list, int step)
    {
        // 获取数据缓存容器
        List<int> result = new List<int>();
        int allCount = totalList.Count;
        int length = step + startIndex;

        for (int i = startIndex; i < length; i++)
        {
            if (startIndex > allCount)
            {
                length = step - result.Count;
                i = 0;
                startIndex = 1;
                PageIndex = 0;
                // 重置之前添加第一个元素
                result.Add(totalList[1]);
                isLimit = true;
            }
            else
            {
                if (i < allCount)
                {
                    // 区分是否之前超出过列表索引,再次添加时,索引+1
                    if (isLimit)
                        result.Add(totalList[i + 1]);
                    else
                        result.Add(totalList[i]);
                }
                else
                {
                    // 超出总数时,重新从第一条开始获取,并添加到容器
                    length = startIndex + step;
                    int addCount = length - allCount;
                    
                    for (int j = 0; j < addCount; j++)
                        result.Add(totalList[j]);
                    break;
                }
            }
        }
        // 重置限制状态
        isLimit = false;
        return result;
    }

结果截图:

        

 

适用与轮播图,一次只位移一个。

有优化空间,有见解的同学,欢迎留言!欢迎指导,不胜感激!

个人学习,不喜勿喷,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据引用内容,el-images轮播图是由ElementUI的el-carousel组件实现的。el-carousel是一个基于Vue.js的轮播图组件,它可以通过设置trigger属性为"click",autoplay属性为false,height属性为"500px"来实现点击切换、不自动播放、并设置高度为500像素的效果。el-carousel的图片来源是通过v-for指令循环遍历bigScreen.images数组中的每个元素,通过动态绑定src属性来显示对应的图片。在这个例子中,bigScreen.images数组包含了多张图片,每张图片都有一个名称和对应的URL。当轮播图的索引发生变化时,会触发handChange方法,并将当前索引作为参数进行打印。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [elementui el-carousel轮播图](https://blog.csdn.net/weixin_43173924/article/details/89841683)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [el-select-tree:ElementUI的el-select与el-tree结合](https://download.csdn.net/download/weixin_42135773/18435535)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值