Unity 3D: Scroll View 刷新

前言:
項目中遇到,讀取一個有限列表,加載更多需要上拉刷新情況。

// 數據刷新請求, 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 的值判斷 是在頂部或是在底部。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章