//reset the layout param, such as column count, item width/height,padding size public void ResetGridViewLayoutParam(int itemTotalCount, GridViewLayoutParam layoutParam) { if (mListViewInited == false) { Debug.LogError("ResetLayoutParam can not use before LoopStaggeredGridView.InitListView are called!"); return; } mScrollRect.StopMovement(); SetListItemCount(0, true); RecycleAllItem(); ClearAllTmpRecycledItem(); mLayoutParam = layoutParam; if (mLayoutParam == null) { Debug.LogError("layoutParam can not be null!"); return; } if (mLayoutParam.CheckParam() == false) { return; } mGroupCount = mLayoutParam.mColumnOrRowCount; mViewPortRectTransform.GetLocalCorners(mViewPortRectLocalCorners); mContainerTrans.anchoredPosition3D = Vector3.zero; mItemTotalCount = itemTotalCount; UpdateLayoutParamAutoValue(); mItemGroupList.Clear(); for (int i = 0; i < mGroupCount; ++i) { StaggeredGridItemGroup group = new StaggeredGridItemGroup(); group.Init(this, mItemTotalCount, i, GetNewItemByGroupAndIndex); mItemGroupList.Add(group); } UpdateContentSize(); }
/* * InitListView method is to initiate the LoopStaggeredGridView component. There are 4 parameters: * itemTotalCount: the total item count in the scrollview, this parameter should be >=0. * layoutParam: this class is very sample, and you need new a GridViewLayoutParam instance and set the values you want. * onGetItemByItemIndex: when an item is getting in the scrollrect viewport, this Action will be called with the item’ index as a parameter, to let you create the item and update its content. * LoopStaggeredGridViewItem is the return value of onGetItemByItemIndex * Every created item has a LoopStaggeredGridViewItem component auto attached */ public void InitListView(int itemTotalCount, GridViewLayoutParam layoutParam, System.Func <LoopStaggeredGridView, int, LoopStaggeredGridViewItem> onGetItemByItemIndex, StaggeredGridViewInitParam initParam = null) { mLayoutParam = layoutParam; if (mLayoutParam == null) { Debug.LogError("layoutParam can not be null!"); return; } if (mLayoutParam.CheckParam() == false) { return; } if (initParam != null) { mDistanceForRecycle0 = initParam.mDistanceForRecycle0; mDistanceForNew0 = initParam.mDistanceForNew0; mDistanceForRecycle1 = initParam.mDistanceForRecycle1; mDistanceForNew1 = initParam.mDistanceForNew1; mItemDefaultWithPaddingSize = initParam.mItemDefaultWithPaddingSize; } mScrollRect = gameObject.GetComponent <ScrollRect>(); if (mScrollRect == null) { Debug.LogError("LoopStaggeredGridView Init Failed! ScrollRect component not found!"); return; } if (mDistanceForRecycle0 <= mDistanceForNew0) { Debug.LogError("mDistanceForRecycle0 should be bigger than mDistanceForNew0"); } if (mDistanceForRecycle1 <= mDistanceForNew1) { Debug.LogError("mDistanceForRecycle1 should be bigger than mDistanceForNew1"); } mScrollRectTransform = mScrollRect.GetComponent <RectTransform>(); mContainerTrans = mScrollRect.content; mViewPortRectTransform = mScrollRect.viewport; mGroupCount = mLayoutParam.mColumnOrRowCount; if (mViewPortRectTransform == null) { mViewPortRectTransform = mScrollRectTransform; } if (mScrollRect.horizontalScrollbarVisibility == ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport && mScrollRect.horizontalScrollbar != null) { Debug.LogError("ScrollRect.horizontalScrollbarVisibility cannot be set to AutoHideAndExpandViewport"); } if (mScrollRect.verticalScrollbarVisibility == ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport && mScrollRect.verticalScrollbar != null) { Debug.LogError("ScrollRect.verticalScrollbarVisibility cannot be set to AutoHideAndExpandViewport"); } mIsVertList = (mArrangeType == ListItemArrangeType.TopToBottom || mArrangeType == ListItemArrangeType.BottomToTop); mScrollRect.horizontal = !mIsVertList; mScrollRect.vertical = mIsVertList; AdjustPivot(mViewPortRectTransform); AdjustAnchor(mContainerTrans); AdjustContainerPivot(mContainerTrans); InitItemPool(); mOnGetItemByItemIndex = onGetItemByItemIndex; if (mListViewInited == true) { Debug.LogError("LoopStaggeredGridView.InitListView method can be called only once."); } mListViewInited = true; mViewPortRectTransform.GetLocalCorners(mViewPortRectLocalCorners); mContainerTrans.anchoredPosition3D = Vector3.zero; mItemTotalCount = itemTotalCount; UpdateLayoutParamAutoValue(); mItemGroupList.Clear(); for (int i = 0; i < mGroupCount; ++i) { StaggeredGridItemGroup group = new StaggeredGridItemGroup(); group.Init(this, mItemTotalCount, i, GetNewItemByGroupAndIndex); mItemGroupList.Add(group); } UpdateContentSize(); }