Android商品规格流式布局运用RecyclerView实现显示和点击效果

本文介绍如何使用RecyclerView结合自定义的FlowLayoutManager和SpaceItemDecoration实现Android中商品规格的流式布局显示,包括布局样式和间距设置。
摘要由CSDN通过智能技术生成

老规矩,先看图:
在这里插入图片描述
布局文件:

TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="规格"
            android:textColor="#ff999999"
            android:textSize="15sp"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="15dp"
            />
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            >

        </android.support.v7.widget.RecyclerView>

代码实现:

			FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
                        //设置每一个item间距
                        recyclerView.addItemDecoration(new SpaceItemDecoration(10);
                        recyclerView.setLayoutManager(flowLayoutManager);
                        specAdapter = new MyGoodsSpecAdapter(getActivity())
                        recyclerView.setAdapter(specAdapter );

**

自定义流式布局样式FlowLayoutManager

**

package com.jkgl.view;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class FlowLayoutManager extends RecyclerView.LayoutManager {
   

    private static final String TAG = FlowLayoutManager.class.getSimpleName();
    final FlowLayoutManager self = this;

    protected int width, height;
    private int left, top, right;
    //最大容器的宽度
    private int usedMaxWidth;
    //竖直方向上的偏移量
    private int verticalScrollOffset = 0;

    public int getTotalHeight() {
   
        return totalHeight;
    }

    //计算显示的内容的高度
    protected int totalHeight = 0;
    private Row row = new Row();
    private List<Row> lineRows = new ArrayList<>();

    //保存所有的Item的上下左右的偏移量信息
    private SparseArray<Rect> allItemFrames = new SparseArray<>();

    public FlowLayoutManager() {
   
        //设置主动测量规则,适应recyclerView高度为wrap_content
        setAutoMeasureEnabled(true);
    }

    //每个item的定义
    public class Item {
   
        int useHeight;
        View view;

        public void setRect(Rect rect) {
   
            this.rect = rect;
        }

        Rect rect;

        public Item(int useHeight, View view, Rect rect
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值