前言:
项目中遇到,读取一个有限列表,加载更多需要上拉刷新情况。
// 数据刷新请求, 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 的值判断 是在顶部或是在底部。
本文介绍了在Unity 3D项目中如何实现Scroll View的上拉刷新功能。通过RequestManager.RankRequest方法进行数据刷新请求,并在Start方法中设置UIEventTrigger的onPointerUp事件监听,当用户点击并抬起时触发onClickUp方法,通过scrollbar的值判断进行上拉或下拉刷新。
4277

被折叠的 条评论
为什么被折叠?



