Android 自定义组件-RigidGridView

作者: 菠萝鱼_lc | 来源:发表于2017-07-06 10:38 被阅读0次

需求有时会用到矩形布局,但是又不太需要滑动,感觉用RecycleView or GridView会有些浪费,之前在某网站写过一篇,但是不完善,今天补一篇,还是那句话,仅供思路上的参考。

View代码:

/**
 * Created by future on 16/8/17.
 */
public class RigidGridView extends RelativeLayout {
    private Context mContext;
    private Activity mActivity;
    private int columnLine = 3;
    private int childWidth = 0;
    private int childHeight = 0;
    private int horSpace = 10;
    private int verSpace = 10;
    private BaseAdapter baseAdapter;

    public RigidGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mActivity = (Activity) this.mContext;
    }

    public void setChildSize(int width, int height) {
        this.childWidth = width;
        this.childHeight = height;
        requestLayout();
    }

    public void setPadding(int horSpace, int verSpace) {
        this.horSpace = horSpace;
        this.verSpace = verSpace;
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        childWidth = parentWidth / columnLine;
        childHeight = childWidth;
        for (int i = 0; i < count; i++) {
            int childWidthMeasureSpec = MeasureSpec
                    .makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
            int childheightMeasureSpec = MeasureSpec
                    .makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
            View child = getChildAt(i);
            child.measure(childWidthMeasureSpec, childheightMeasureSpec);
        }
        int newParentHeight = childHeight * getLine() + verSpace * (getLine() - 1);
        setMeasuredDimension(parentWidth, newParentHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int left = 0;
        int top = 0;
        for(int i = 0; i < childCount; i++) {
            if(isNewLine(i)) {
                top += childHeight + verSpace;
                left = 0;
            }
            View view = getChildAt(i);
            view.layout(left, top, left + childWidth, childHeight + top);
            left += childWidth + horSpace;
        }
    }

    private int getLine() {
        int line = 0;
        line = getChildCount() / 2;
        if(getChildCount() % columnLine != 0) {
            line += 1;
        }
        return line;
    }

    private boolean isNewLine(int index) {
        if (index != 0 && index % columnLine == 0) {
            return true;
        }
        return false;
    }

    private int getRowCount() {
        int line = (getChildCount()) / columnLine;
        if ((getChildCount()) % columnLine != 0) {
            line += 1;
        }
        return line;
    }

    private void onChange() {
        removeAllViews();
        if (baseAdapter != null) {
            for (int i = 0; i < baseAdapter.getItemCount(); i++) {
                RecyclerView.ViewHolder viewHolder = baseAdapter.createViewHolder(this, baseAdapter.getItemViewType(i));
                baseAdapter.onBindViewHolder(viewHolder, i);
                View view = viewHolder.itemView;
                addView(view);

            }
            postInvalidate();
        }
    }

    public void setAdapter(BaseAdapter baseAdapter) {
        this.baseAdapter = baseAdapter;
        baseAdapter.registerAdapterDataObserver(mObserver);
        baseAdapter.notifyDataSetChanged();
    }

    public void setColumnLine(int columnLine) {
        this.columnLine = columnLine;
        invalidate();
    }

    private RecyclerView.AdapterDataObserver mObserver = new RecyclerView.AdapterDataObserver() {
        public void onChanged() {
            onChange();
        }
    };

用法:

 mRigidGridView = (RigidGridView) findViewById(R.id.rigid_grid);
 mRigidAdapter = new RigidAdapter(this);
 mRigidAdapter.setDatas(testList());
 mRigidGridView.setAdapter(mRigidAdapter);

很多变量没有抽取成xml参数,用的时候完全可以抽取出来。

相关文章

网友评论

    本文标题:Android 自定义组件-RigidGridView

    本文链接:https://www.haomeiwen.com/subject/dxchhxtx.html