Skip to content

Unity-GamerFrameWork/SupperScrollView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

无限滚动解决方案

1.UltimateCleanGUIPack:有一套完成的UI模板
2.SuperScrollView:无限滚动解决方案

一、介绍

  1. 什么是无限滚动?
    无限滚动是指滚动列表中的Item对象,支持无限回收在利用的循环滚动列表。
  2. 作用
    利用对象回收利用的机制,提升在进行大批量数据滚动显示时的性能。

二、核心API

InitListView: 初始化滚动列表
image

SetListItemCount: 重新设置滚动列表个数。
数据增减必须调用此接口,否则会出现 item 索引与数据不一致和其他显示 Bug。
image

RefreshAllShownItem: 更新所有可见的 item 最新数据
image

MovePanelToItemIndex: 直接瞬移到目标索引 Item 处
image

SetSnapTargetItemIndex: 以缓动动画的形式平滑滚动到目标索引 Item 处
image


三、具体的使用方法

  1. 找到要使用的模板(横向/纵向 ListView、网格 Grid 等)
  2. 复制对应的 LoopListView,设置对应的 item 和 Padding
    image
  3. 代码示例:
using SuperScrollView;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RankWindow : MonoBehaviour
{
    public LoopListView2 loopListView;
    private List<RankData> mRankList;

    private void Awake()
    {
        RefreshListView();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            RankDataManager.Instance.AddRankData(50);
            RefreshListView();
        }
        if (Input.GetKeyDown(KeyCode.W))
        {
            RankDataManager.Instance.AddRankData(50);
            RefreshListView(false);
        }
        if (Input.GetKeyDown(KeyCode.R))
        {
            MoveItemToIndex(50);
        }
        if (Input.GetKeyDown(KeyCode.T))
        {
            MoveItemToIndexScroll(50);
        }
    }

    public void RefreshListView(bool resPos = true)
    {
        mRankList = RankDataManager.Instance.RankDataList;
        if (!loopListView.ListViewInited)
        {
            loopListView.InitListView(mRankList.Count, OnGetItemByIndex);
        }
        else
        {
            loopListView.SetListItemCount(mRankList.Count, resPos);
            loopListView.RefreshAllShownItem();
        }
    }

    public void MoveItemToIndex(int index)
    {
        loopListView.MovePanelToItemIndex(index, 0);
        loopListView.RefreshAllShownItem();
    }

    public void MoveItemToIndexScroll(int index)
    {
        loopListView.SetSnapTargetItemIndex(50, 30000);
        loopListView.RefreshAllShownItem();

        loopListView.mOnSnapItemFinished = (listView, item) =>
        {
            loopListView.RefreshAllShownItem();
        };
    }

    LoopListViewItem2 OnGetItemByIndex(LoopListView2 listView, int index)
    {
        if (index < 0 || index >= mRankList.Count)
            return null;

        RankData rankData = mRankList[index];
        if (rankData == null)
            return null;

        LoopListViewItem2 item = listView.NewListViewItem("RankItem");
        RankItem itemScript = item.GetComponent<RankItem>();

        if (!item.IsInitHandlerCalled)
        {
            item.IsInitHandlerCalled = true;
            itemScript.Init();
        }

        itemScript.SetItemData(rankData, index);
        return item;
    }
}

About

无限滚动解决方案

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published