天天记录 - 自定义ViewGroup使用Adapter提供视图
来源:互联网 发布:淘宝实名制 编辑:程序博客网 时间:2024/06/05 20:22
例子下载地址
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// ===========================================================@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int width = MeasureSpec.getSize(widthMeasureSpec);int height = MeasureSpec.getSize(heightMeasureSpec);// 从Adapter获取所有的child Viewint count = mAdapter.getCount();for (int i = 0; i < count; i++) {View view = mAdapter.getView(i, null, null);// 添加到ViewGroupaddView(view);int measuredWidth = view.getMeasuredWidth();int measuredHeight = view.getMeasuredHeight();view.measure(measuredWidth, measuredHeight);}setMeasuredDimension(width, height);}@Overrideprotected 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还有更好的做法
- 天天记录 - 自定义ViewGroup使用Adapter提供视图
- 自定义viewgroup(6)--使用adapter适配数据
- 天天记录 - Android 使用Canvas Transalte方法控制视图移动
- Android自定义ViewGroup记录
- ListView自定义Adapter中getView(int position, View convertView, ViewGroup parent) 使用
- ListView自定义Adapter中getView(int position, View convertView, ViewGroup parent) 使用
- 使用ViewGroup自定义数字键盘
- 使用 ViewGroup 自定义 SlidingLayout
- 自定义ViewGroup的使用
- ListView使用自定义Adapter
- 自定义Adapter的使用
- Android 自定义Adapter动态更新ListView视图
- ListView 自定义Adapter 实现多视图Item
- 【Android】快速开发偷懒必备,一句话搞定所有ViewGroup的Adapter . 支持自定义ViewGroup
- 天天记录
- 天天记录 - 使用VelocityTracker计算滑动速率
- 使用Adapter为ListView提供数据
- 使用Adapter提供的convertView优化Listview
- UVa 10085 - The most distant state
- JQuery下关于$.Ready()的分析
- Java程序如何生成Jar、exe及安装文件
- 快速排序1--HoardSort/Sort in CLRS
- android 2D skia库 简单应用
- 天天记录 - 自定义ViewGroup使用Adapter提供视图
- FFMPEG for android 文章汇总
- 编写高效的android代码(译文)
- 同类炸弹Ⅱ(9900炮) 游戏机 玩法技巧
- 工作趣事 之 学以致用-把人变成懒人
- 一个30年老程序员的回顾(二)
- android skia 使用实例
- RPC和Socket的区别
- C++成员访问权限