即上面的(Android LinearLayout实现多行多列)(http://blog.csdn.net/u010947098/article/details/45572849)
再加入到项目中后,有了更好的编码,所以共享给大家,嘻嘻.
1.这个是第一个例子,
/***
* 创建多行多列的线性布局
*
* @param vipDoctor
* 医生信息
* @param selectedPosition
* 圆形菜单选项
*/
private void createManyLineLinearLayout(VipDoctor vipDoctor,
int selectedPosition) {
if (vipDoctor == null) {
return;
}
// 删除所有子视图
mServiceContentLinear.removeAllViews();
// 获取服务内容数据
ArrayList<VipContent> vipContents = vipDoctor.getPriceList()
.get(selectedPosition).getContentList();
// 得到数据总条数
int size = vipContents.size();
// 总共显示行数
int rows = 0;
// 判断,是否被整除
if (size % mRowChildCound == 0) {
// 能被整除,行数 = 总条数 / 列数
rows = size / mRowChildCound;
} else {
// 不能被整除,行数 = 总条数 / 列数 + 1
rows = size / mRowChildCound + 1;
}
// 循环创建列
for (int row = 0; row < rows; row++) {
// 创建每行显示的列
createColumnsLinearLayout(row, vipContents);
}
}
/***
* 创建每行显示多少列(每行都会调用这个方法创建列)
*
* @param row
* 每行
* @param vipContents
* 服务内容集合
*/
private void createColumnsLinearLayout(int row,
ArrayList<VipContent> vipContents) {
if (vipContents == null) {
return;
}
// 获取行布局
ViewGroup rootView = (ViewGroup) mLayoutInflater.inflate(
R.layout.item_row_service_content, null, false);
// 循环为每一列添加数据
for (int index = 0; index < mRowChildCound; index++) {
// 得到每一列的布局
View convertView = rootView.getChildAt(index);
// 获取组件
TextView tv = (TextView) convertView
.findViewById(R.id.item_member_service_content_option);
// 计算第几列,对应数据集合的索引
int position = row * mRowChildCound + index;
// 判断总列数是否大于数据的总数
if (position >= vipContents.size()) {
// 大于,隐藏剩下的
tv.setVisibility(View.GONE);
} else {
VipContent vipContent = vipContents.get(position);
// 小于,显示
// 保存到tag中
tv.setTag(position);
// setText
tv.setText(vipContent.getServiceName());
// 判断是否显示绿色背景
if (TextUtils.isEmpty(vipContent.getVpsId())) {
// 灰色
tv.setBackgroundResource(R.drawable.member_service_content_bg);
} else {
// 绿色
tv.setBackgroundResource(R.drawable.member_service_content_bg_green);
}
// 点击事件
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LogUtil.i("main", v.getTag().toString());
}
});
}
}
// 将这一行添加到主布局中
mServiceContentLinear.addView(rootView);
mServiceContentLinear.setFocusable(false);
}
那个,过了些日子了,实现思路有些忘了,注释很清楚,自己看吧.....
2.这是第二个例子,
/**
* 生成服务内容的内容的所有行数据
* @param serviceContent
*/
private void createEntryLine(String[] serviceContent){
if(serviceContent == null || serviceContent.length <= 0){
return;
}
for (int i = 0; i < serviceContent.length; i++) {
if(TextUtils.isEmpty(serviceContent[i])){
continue;
}
ViewGroup rootView = (ViewGroup) mLayoutInflater.inflate(R.layout.item_member_details_entry, null, false);
if(rootView != null){
TextView tv = (TextView) rootView.getChildAt(1);
tv.setText(serviceContent[i]);
mServiceContentLinear.addView(rootView);
}
}
好了,谢谢大家了.....