Search in sources :

Example 1 with BaseSectionQuickAdapter

use of com.chad.library.adapter.base.BaseSectionQuickAdapter 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)

Aggregations

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