需求有時(shí)會(huì)用到矩形布局,但是又不太需要滑動(dòng)昵济,感覺(jué)用RecycleView or GridView會(huì)有些浪費(fèi)智绸,之前在某網(wǎng)站寫過(guò)一篇,但是不完善访忿,今天補(bǔ)一篇瞧栗,還是那句話,僅供思路上的參考海铆。
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);
很多變量沒(méi)有抽取成xml參數(shù)沼溜,用的時(shí)候完全可以抽取出來(lái)。