Unity 3D: Scroll View 刷新

本文介绍了在Unity 3D项目中如何实现Scroll View的上拉刷新功能。通过RequestManager.RankRequest方法进行数据刷新请求,并在Start方法中设置UIEventTrigger的onPointerUp事件监听,当用户点击并抬起时触发onClickUp方法,通过scrollbar的值判断进行上拉或下拉刷新。
部署运行你感兴趣的模型镜像

前言:
项目中遇到,读取一个有限列表,加载更多需要上拉刷新情况。

// 数据刷新请求, arg0: 当前列表名, arg1: 刷新请求 开始序号, arg2: 刷新请求 结束序号
RequestManager.RankRequest(temp, first_id, last_id);

UIEventTrigger.Get(transform.gameObject).onPointerUp = onClickUp; // 在 Start 方法里 添加这句

// 构造 onClickUp() 方法 — 点击 抬起事件监听
void onClickUp(){

}

例子:

        bool isDragup = false;
        bool isValueChanged = false;
        void onClickUp(PointerEventData data)
        {
            ScrollRect scroll = transform.FindChild("rankLayer/sv_list").GetComponent<ScrollRect>();
            float flag = scroll.content.localPosition.y;
            scrollbar.onValueChanged.AddListener(delegate
            {
                // 根据下拉高度和滚动条的值 作判断
                if (flag <= 0 || flag >= 800 || scrollbar.value == 0 || scrollbar.value == 1)
                {
                    isValueChanged = true;
                }
                else
                {
                    isValueChanged = false;
                }

                //isValueChanged = (scrollbar.value == 0 || scrollbar.value == 1) ? true : false;

            });
            Debug.Log("isValueChanged:" + isValueChanged+",flag:"+flag);
            int count = lstItem.Count;
            // 上拉刷新 (加载更多)
            if (scrollbar.value <= 0 && isValueChanged)
            {
                Debug.Log("scroll bar value:"+scrollbar.value+", temp:"+temp);
                // 若当前列表数目低于20 不刷新
                if (count < 20)
                {
                    return;
                }
                else
                {
                    first_id = last_id + 1;     //count;
                    last_id = last_id + 20;     //count - 1 + 20;
                    // 等于100 (最大值)时,不刷新数据
                    if (last_id == 100)
                    {
                        return;
                    }
                    else
                    {
                        RequestManager.RankRequest(temp, first_id, last_id);
                        isRefresh = true;
                        isValueChanged = false;
                        onRefresh(temp);
                    }
                    isDragup = true;
                    Debug.Log(" === on drag up === :first:" + first_id + ",last id:" + last_id + ", count:" + count);
                }

            }
            // 下拉刷新
            else if (scrollbar.value >=1 && isValueChanged)
            {
                if (count < 20)
                {
                    int diff = 20 - (last_id + 1);        // 20 - count;
                    first_id = (last_id + 1) - 20 + diff; // count - 20 +diff
                    last_id = last_id + diff;         // count - 1 + diff;
                    RequestManager.RankRequest(temp, first_id, last_id);
                    isRefresh = false;
                }
                else
                {
                    first_id = (last_id + 1) - 20;  // count - 20;
                    last_id = (last_id + 1) - 1;    // count - 1;
                    if (first_id == 0 )
                    {
                        return; // 首页不刷新
                    }
                    else
                    {
                        RequestManager.RankRequest(temp, first_id, last_id);
                        isRefresh = false;
                    }
                }
                isValueChanged = false;
                Debug.Log(" on drop down, first id:" + first_id + ",last id:" + last_id + ", count:" + count);
            }

        }

        // Update is called once per frame
        void Update()
        {
            if (isDragup)
            {
                scrollbar.value = 1.0f; // 从 顶部 开始显示 
                Debug.Log("on value set:" + scrollbar.value);
                isDragup = false;
                isValueChanged = false;
            }
        }

这里只是简单实现了 上拉和下拉刷新,通scrollbar 的值判断 是在顶部或是在底部。

您可能感兴趣的与本文相关的镜像

EmotiVoice

EmotiVoice

AI应用

EmotiVoice是由网易有道AI算法团队开源的一块国产TTS语音合成引擎,支持中英文双语,包含2000多种不同的音色,以及特色的情感合成功能,支持合成包含快乐、兴奋、悲伤、愤怒等广泛情感的语音。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值