Example #1
0
        //public void Logi(string msg)
        //{
        //    Log.Info(TAG, msg);
        //}

        //public void Loge(string err)
        //{
        //    Log.Error(TAG, err);
        //}
        //--- 处理布局 ----------------------------------------------------------------------------------

        /**
         * 布局子View
         *
         * @param recycler Recycler
         * @param state    State
         */

        public override void OnLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)
        {
            Log.Info(TAG, "Item onLayoutChildren");
            Log.Info(TAG, "Item onLayoutChildren isPreLayout = " + state.IsPreLayout);
            Log.Info(TAG, "Item onLayoutChildren isMeasuring = " + state.IsMeasuring);
            Log.Info(TAG, "Item onLayoutChildren state = " + state);

            // 如果是 preLayout 则不重新布局
            if (state.IsPreLayout || !state.DidStructureChange())
            {
                return;
            }

            if (ItemCount == 0)
            {
                RemoveAndRecycleAllViews(recycler);
                // 页面变化回调
                SetPageCount(0);
                SetPageIndex(0, false);
                return;
            }
            else
            {
                SetPageCount(GetTotalPageCount());
                SetPageIndex(GetPageIndexByOffset(), false);
            }

            // 计算页面数量
            int mPageCount = ItemCount / mOnePageSize;

            if (ItemCount % mOnePageSize != 0)
            {
                mPageCount++;
            }

            // 计算可以滚动的最大数值,并对滚动距离进行修正
            if (CanScrollHorizontally())
            {
                mMaxScrollX = (mPageCount - 1) * GetUsableWidth();
                mMaxScrollY = 0;
                if (mOffsetX > mMaxScrollX)
                {
                    mOffsetX = mMaxScrollX;
                }
            }
            else
            {
                mMaxScrollX = 0;
                mMaxScrollY = (mPageCount - 1) * GetUsableHeight();
                if (mOffsetY > mMaxScrollY)
                {
                    mOffsetY = mMaxScrollY;
                }
            }

            // 接口回调
            // setPageCount(mPageCount);
            // setPageIndex(mCurrentPageIndex, false);

            Logi("count = " + ItemCount);

            if (mItemWidth <= 0)
            {
                mItemWidth = GetUsableWidth() / mColumns;
            }
            if (mItemHeight <= 0)
            {
                mItemHeight = GetUsableHeight() / mRows;
            }

            mWidthUsed  = GetUsableWidth() - mItemWidth;
            mHeightUsed = GetUsableHeight() - mItemHeight;

            // 预存储两页的View显示区域
            for (int i = 0; i < mOnePageSize * 2; i++)
            {
                GetItemFrameByPosition(i);
            }

            if (mOffsetX == 0 && mOffsetY == 0)
            {
                // 预存储View
                for (int i = 0; i < mOnePageSize; i++)
                {
                    if (i >= ItemCount)
                    {
                        break;                 // 防止数据过少时导致数组越界异常
                    }
                    View view = recycler.GetViewForPosition(i);
                    AddView(view);
                    MeasureChildWithMargins(view, mWidthUsed, mHeightUsed);
                }
            }

            // 回收和填充布局
            RecycleAndFillItems(recycler, state, true);
        }