public void ShowLen(int len_, int jumpTo_ = -1, JumpPosType jumpType_ = JumpPosType.CENTER) { if (tmp_seq == null) { tmp_seq = new List <int>(); } ShowList(ListUtil.GenSequence(len_, 1, 1, tmp_seq), jumpTo_, jumpType_); }
public void ShowList(IList dataList_, int jumpTo_ = -1, JumpPosType jumpType_ = JumpPosType.CENTER) { m_dataList = dataList_; m_item2data.Clear(); //清除原来的数据 GenLayoutItems(m_dataList.Count); RefreshContentSize(); //修改content尺寸 if (jumpTo_ >= 0) { JumpToIndex(jumpTo_, jumpType_); } __ShowList(); m_dataLenLast = m_dataList.Count; }
/// <summary> /// 跳转到指定序号 /// </summary> /// <param name="index_"></param> /// <param name="posType_"></param> /// <returns></returns> public bool JumpToIndex(int index_, JumpPosType posType_) { //if (!m_scrollView.NeedScroll()) // //不需要滚动 // return false; if (index_ < 0 || index_ > m_dataList.Count - 1) { //超出数据范围 return(false); } Vector2 contentPos = m_contentTrans.anchoredPosition; Vector2 maskSize = m_scrollView.maskTrans.sizeDelta; LayoutItem layoutItem = GetLayoutItem(index_); Rect rect = layoutItem.rect; KScrollView.ScrollDir dir = direction; if (dir == KScrollView.ScrollDir.vertical) { float yMin = 0; float yMax = m_contentSize.y - maskSize.y; float y; switch (posType_) { case JumpPosType.TOP: //列表项位于顶部 y = -rect.yMax; //y是负数 break; case JumpPosType.CENTER: //列表项位于中间 y = -rect.center.y - maskSize.y * 0.5f; break; default: //列表项位于底部 y = -rect.yMin - maskSize.y; break; } contentPos.y = Mathf.Clamp(y, yMin, yMax); } else { float xMin = maskSize.x - m_contentSize.x; float xMax = 0; float x; switch (posType_) { case JumpPosType.TOP: //列表项位于顶部 x = -rect.xMin; break; case JumpPosType.CENTER: //列表项位于中间 x = -rect.center.x + maskSize.x * 0.5f; break; default: //列表项位于底部 x = -rect.xMax + maskSize.x; break; } contentPos.x = Mathf.Clamp(x, xMin, xMax); } m_scrollView.StopMovement(); //停止滑动 m_contentTrans.anchoredPosition = contentPos; return(true); }