Search in sources :

Example 1 with SectionEntity

use of com.chad.library.adapter.base.entity.SectionEntity in project BaseRecyclerViewAdapterHelper by CymChad.

the class GridSectionAverageGapItemDecoration method getItemOffsets.

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (parent.getLayoutManager() instanceof GridLayoutManager && parent.getAdapter() instanceof BaseSectionQuickAdapter) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        BaseSectionQuickAdapter<SectionEntity, BaseViewHolder> adapter = (BaseSectionQuickAdapter) parent.getAdapter();
        if (mAdapter != adapter) {
            setUpWithAdapter(adapter);
        }
        int spanCount = layoutManager.getSpanCount();
        int position = parent.getChildAdapterPosition(view) - mAdapter.getHeaderLayoutCount();
        SectionEntity entity = adapter.getItem(position);
        if (entity == null || entity.isHeader()) {
            // 不处理header
            outRect.set(0, 0, 0, 0);
            // Log.w("GridAverageGapItem", "pos=" + position + "," + outRect.toShortString());
            return;
        }
        Section section = findSectionLastItemPos(position);
        if (gapHSizePx < 0 || gapVSizePx < 0) {
            transformGapDefinition(parent, spanCount);
        }
        outRect.top = gapVSizePx;
        outRect.bottom = 0;
        // 下面的visualPos为单个Section内的视觉Pos
        int visualPos = position + 1 - section.startPos;
        if (visualPos % spanCount == 1) {
            // 第一列
            outRect.left = sectionEdgeHPaddingPx;
            outRect.right = eachItemHPaddingPx - sectionEdgeHPaddingPx;
        } else if (visualPos % spanCount == 0) {
            // 最后一列
            outRect.left = eachItemHPaddingPx - sectionEdgeHPaddingPx;
            outRect.right = sectionEdgeHPaddingPx;
        } else {
            outRect.left = gapHSizePx - (eachItemHPaddingPx - sectionEdgeHPaddingPx);
            outRect.right = eachItemHPaddingPx - outRect.left;
        }
        if (visualPos - spanCount <= 0) {
            // 第一行
            outRect.top = sectionEdgeVPaddingPx;
        }
        if (isLastRow(visualPos, spanCount, section.getCount())) {
            // 最后一行
            outRect.bottom = sectionEdgeVPaddingPx;
        // Log.w("GridAverageGapItem", "last row pos=" + position);
        }
    // Log.w("GridAverageGapItem", "pos=" + position + ",vPos=" + visualPos + "," + outRect.toShortString());
    } else {
        super.getItemOffsets(outRect, view, parent, state);
    }
}
Also used : BaseSectionQuickAdapter(com.chad.library.adapter.base.BaseSectionQuickAdapter) BaseViewHolder(com.chad.library.adapter.base.viewholder.BaseViewHolder) GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) SectionEntity(com.chad.library.adapter.base.entity.SectionEntity)

Example 2 with SectionEntity

use of com.chad.library.adapter.base.entity.SectionEntity in project BaseRecyclerViewAdapterHelper by CymChad.

the class GridSectionAverageGapItemDecoration method markSections.

private void markSections() {
    if (mAdapter != null) {
        BaseSectionQuickAdapter<SectionEntity, BaseViewHolder> adapter = mAdapter;
        mSectionList.clear();
        SectionEntity sectionEntity = null;
        Section section = new Section();
        for (int i = 0, size = adapter.getItemCount(); i < size; i++) {
            sectionEntity = adapter.getItem(i);
            if (sectionEntity != null && sectionEntity.isHeader()) {
                // 找到新Section起点
                if (section != null && i != 0) {
                    // 已经有待添加的section
                    section.endPos = i - 1;
                    mSectionList.add(section);
                }
                section = new Section();
                section.startPos = i + 1;
            } else {
                section.endPos = i;
            }
        }
        // 处理末尾情况
        if (!mSectionList.contains(section)) {
            mSectionList.add(section);
        }
    // Log.w("GridAverageGapItem", "section list=" + mSectionList);
    }
}
Also used : BaseViewHolder(com.chad.library.adapter.base.viewholder.BaseViewHolder) SectionEntity(com.chad.library.adapter.base.entity.SectionEntity)

Aggregations

SectionEntity (com.chad.library.adapter.base.entity.SectionEntity)2 BaseViewHolder (com.chad.library.adapter.base.viewholder.BaseViewHolder)2 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)1 BaseSectionQuickAdapter (com.chad.library.adapter.base.BaseSectionQuickAdapter)1