RecyclerView 实现网格布局 item四周有分割线 展示分类信息

效果如下:

RecyclerView 实现网格布局 item四周有分割线 展示分类信息_第1张图片

每一个item核心代码:

public class HistoryItemView extends LinearLayout {

    private  ScrollViewWithRecyclerView historyRv;
    private Context context;
    private List data;
    private String title;
    private RecyclerView.Adapter adapter;
    private TextView brandTv;
    private TextView collapseTv;
    private HistoryData historyData;

    public HistoryItemView(Context context) {
        this(context, null);
    }

    public HistoryItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HistoryItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    private void initView() {
        setOrientation(VERTICAL);
        View view = inflate(getContext(), R.layout.layout_item_history, this);
        brandTv = (TextView)view.findViewById(R.id.brand);
        collapseTv = (TextView)view.findViewById(R.id.collapse);

        historyRv = view.findViewById(R.id.historyRv);
        initAdapter();

        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        historyRv.setLayoutManager(gridLayoutManager);
        GridItemDivider gridLayoutDivider = new GridItemDivider( 2, Color.parseColor("#CCCCCC"));
        historyRv.addItemDecoration(gridLayoutDivider);
        historyRv.setAdapter(adapter);

        collapseTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isOpen = historyData.isOpen();
                String text = isOpen ? "展开" : "收起";
                collapseTv.setText(text);
                historyData.setOpen(!isOpen);
                adapter.notifyDataSetChanged();
            }
        });

    }

    private void initAdapter() {
        final LayoutInflater mInflater = LayoutInflater.from(context);

        adapter = new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
                View itemView = mInflater.inflate(R.layout.table_row_item, parent, false);
                return new HistoryViewHolder(itemView);
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                HistoryViewHolder holder = (HistoryViewHolder) viewHolder;
                holder.bindData(i);

            }

            @Override
            public int getItemCount() {
                if( data.size() >= 6 && !historyData.isOpen()){
                    return 6;
                }
                return data.size();
            }
        };
    }

    public void bindData(HistoryData historyData) {
        this.historyData = historyData;
        data = historyData.getContentList();
        title = historyData.getTitile();
        brandTv.setText(title);
        adapter.notifyDataSetChanged();
    }

    private class HistoryViewHolder extends RecyclerView.ViewHolder {
        TextView historyTv;

        public HistoryViewHolder(View itemView) {
            super(itemView);
            historyTv = itemView.findViewById(R.id.history);
        }

        public void bindData(int pos) {
            String content = data.get(pos);
            historyTv.setText(content);

        }
    }
}

布局文件:




    
        


        
    



  

  

每一个item的分割线:

public class GridItemDivider extends RecyclerView.ItemDecoration  {
    private String TAG = getClass().getSimpleName();
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
    private Drawable divider;

    public GridItemDivider(Context context) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        divider = a.getDrawable(0);
        a.recycle();
    }

    public GridItemDivider(Drawable drawable) {
        divider = drawable;
    }

    public GridItemDivider(int height, int color) {
        GradientDrawable shapeDrawable = new GradientDrawable();
        shapeDrawable.setColor(color);
        shapeDrawable.setShape(GradientDrawable.RECTANGLE);
        shapeDrawable.setSize(height, height);
        divider = shapeDrawable;
    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

        drawHorizontal(c, parent);
        drawVertical(c, parent);

    }

    private int getSpanCount(RecyclerView parent) {
        // 列数
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {

            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager)
                    .getSpanCount();
        }
        return spanCount;
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount(); //获取可见item的数量
        int spanCount = getSpanCount(parent);
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin
                    + divider.getIntrinsicWidth();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + divider.getIntrinsicHeight();
            divider.setBounds(left, top, right, bottom);
            divider.draw(c);
            if (i < spanCount) { //画第一行顶部的分割线
                drawHorizontalForFirstRow(c, child);
            }
        }
    }

    private void drawHorizontalForFirstRow(Canvas c, View child) {
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        int left = child.getLeft() - params.leftMargin - divider.getIntrinsicWidth();
        int top = child.getTop() - params.topMargin - divider.getIntrinsicHeight();
        int right = child.getRight() + params.rightMargin + divider.getIntrinsicWidth();
        int bottom = top + divider.getIntrinsicHeight();
        divider.setBounds(left, top, right, bottom);
        divider.draw(c);
    }

    private void drawVerticalForFirstColum(Canvas c, View child) {
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        int left = child.getLeft() - params.leftMargin - divider.getIntrinsicWidth();
        int top = child.getTop() - params.topMargin;
        int right = child.getLeft() - params.leftMargin;
        int bottom = top + child.getHeight() + divider.getIntrinsicHeight();
        divider.setBounds(left, top, right, bottom);
        divider.draw(c);
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getRight() + params.rightMargin;
            final int right = left + divider.getIntrinsicWidth();
            divider.setBounds(left, top, right, bottom);
            divider.draw(c);
            if (isFirstColum(parent, i, getSpanCount(parent))) { //画第一列左边分割线
                drawVerticalForFirstColum(c, child);
            }
        }
    }

    private boolean isLastColum(RecyclerView parent, int pos, int spanCount,
                                int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
            {
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
                {
                    return true;
                }
            } else {
                childCount = childCount - childCount % spanCount;
                if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
                    return true;
            }
        }
        return false;
    }

    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
                              int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
                return true;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                childCount = childCount - childCount % spanCount;
                // 如果是最后一行,则不需要绘制底部
                if (pos >= childCount)
                    return true;
            } else
            {
                // 如果是最后一行,则不需要绘制底部
                if ((pos + 1) % spanCount == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    //是否为第一列
    private boolean isFirstColum(RecyclerView parent, int pos, int spanCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) { //网格布局
            if ((pos + 1) % spanCount == 1) {
                return true;
            }
        }
        return false;
    }

    //是否为第一行
    private boolean isFirstRaw(int pos, int spanCount) {
        if (pos < spanCount) {
            return true;
        }
        return false;
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition,
                               RecyclerView parent) {
        int spanCount = getSpanCount(parent); //列数

        if (itemPosition == 0) { //第一行第一个,四边都画
            outRect.set(divider.getIntrinsicWidth(), divider.getIntrinsicHeight(),
                    divider.getIntrinsicWidth(), divider.getIntrinsicHeight());
        } else if (isFirstRaw(itemPosition, spanCount)) { //第一行,画上下右三边
            outRect.set(0, divider.getIntrinsicHeight(), divider.getIntrinsicWidth(), divider.getIntrinsicHeight());
        } else if (isFirstColum(parent, itemPosition, spanCount)) { //第一列,画左右下三边
            outRect.set(divider.getIntrinsicWidth(), 0, divider.getIntrinsicWidth(), divider.getIntrinsicHeight());
        } else { //其他,画右下两边
            outRect.set(0, 0, divider.getIntrinsicWidth(), divider.getIntrinsicHeight());
        }

    }

}

 

demo下载地址 

 

你可能感兴趣的:(Android)