1. 创建ViewGroup继承自ViewGroup,覆写onMeasure和onLayout
public class CustomViewGroup extends ViewGroup {
// ===========================================================
// Fields
// ===========================================================
private BaseAdapter mAdapter;
// ===========================================================
// Constructors
// ===========================================================
public CustomViewGroup(Context context) {
super(context);
}
public CustomViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
// ===========================================================
// Getter & Setter
// ===========================================================
public void setAdapter(BaseAdapter pAdapter) {
mAdapter = pAdapter;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
// 从Adapter获取所有的child View
int count = mAdapter.getCount();
for (int i = 0; i < count; i++) {
View view = mAdapter.getView(i, null, null);
// 添加到ViewGroup
addView(view);
int measuredWidth = view.getMeasuredWidth();
int measuredHeight = view.getMeasuredHeight();
view.measure(measuredWidth, measuredHeight);
}
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (changed == false) {
return;
}
// 用于纵向显示
// 累加每个视图的高度,便于下一个视图获取top顶点值
int totalHeight = 0;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
int measuredHeight = childView.getMeasuredHeight();
int measuredWidth = childView.getMeasuredWidth();
// 设置每个View的显示位置
childView.layout(left, totalHeight, measuredWidth, totalHeight + measuredHeight);
totalHeight += measuredHeight;
}
}
}
2. 在Activity创建视图,数据,Adapter
public class CustomViewGroup_AdapterActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 创建视图
CustomViewGroup customViewGroup = new CustomViewGroup(this);
customViewGroup.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// Activity绑定View
setContentView(customViewGroup);
// 创建数据
ArrayList<TextViewInfo> arrayList = new ArrayList<TextViewInfo>();
arrayList.add(new TextViewInfo("自定义", Color.BLUE));
arrayList.add(new TextViewInfo("ViewGroup", Color.YELLOW));
arrayList.add(new TextViewInfo("Adapter", Color.RED));
// 创建Adapter
CustomAdapter customAdapter = new CustomAdapter(this, arrayList);
customViewGroup.setAdapter(customAdapter);
}
/**
* 存放TextView的文本和背景颜色实体
*/
public class TextViewInfo {
private int mColor;
private String mName;
public TextViewInfo(String pName, int pColor) {
mName = pName;
mColor = pColor;
}
public int getColor() {
return mColor;
}
public void setColor(int mColor) {
this.mColor = mColor;
}
public String getName() {
return mName;
}
public void setName(String mName) {
this.mName = mName;
}
}
}
效果图:
扩展:
查看下ListView源码
此例子虽然实现了期望的功能,总感觉onMeasure和onLayout还有更好的做法