动态 TableLayout 内存 溢出

Earliest-Mlr 2016-06-16 11:02:57
这是一个车型配置的对比表格布局,因为我是动态创建的,在数据量很大的时候报内存溢出,大概有10000+的数据,我该怎么优化

//初始化控件
TableLayout tl_config_table = (TableLayout) findViewById(R.id.tl_config_table);
tl_config_table.setStretchAllColumns(true);//设置所有列可伸缩

tl_config_table.removeAllViews();
//动态创建TableRow
TextView tv_config_type = (TextView) findViewById(R.id.tv_config_type);
HScrollView hsv_config_scroll = (HScrollView) findViewById(R.id.hsv_config_scroll);
LinearLayout ll_config_content = (LinearLayout) findViewById(R.id.ll_config_content);
ll_config_content.removeAllViews();

hScrollViews.add(hsv_config_scroll);
tv_config_type.setVisibility(View.VISIBLE);
tv_config_type.setText("车型");

int rankCount = contentHashMaps.size() > 25 ? 25 : contentHashMaps.size();
int i = 0;
int aaa = 0;
for (HashMap<String, Object> topHashMap : contentHashMaps){
if(rankCount == i) break;
//顶部Item
View topItemView = LayoutInflater.from(this).inflate(R.layout.config_table_row_item, null);
RelativeLayout rl_config_row_top_item = (RelativeLayout) topItemView.findViewById(R.id.rl_config_row_top_item);
TextView tv_config_row_top_text = (TextView) topItemView.findViewById(R.id.tv_config_row_top_text);
ImageView iv_config_row_top_delete = (ImageView) topItemView.findViewById(R.id.iv_config_row_top_delete);
ImageView iv_config_row_top_add = (ImageView) topItemView.findViewById(R.id.iv_config_row_top_add);
View v_config_row_divide = topItemView.findViewById(R.id.v_config_row_divide);

v_config_row_divide.setVisibility(View.VISIBLE);
rl_config_row_top_item.setVisibility(View.VISIBLE);
if (topHashMap != null && i != (rankCount - 1)) {//正常显示数据
rl_config_row_top_item.setVisibility(View.VISIBLE);
iv_config_row_top_delete.setVisibility(View.VISIBLE);
iv_config_row_top_add.setVisibility(View.GONE);
tv_config_row_top_text.setText(topHashMap.get("subject").toString());
//监听删除图标的单击事件
iv_config_row_top_delete.setOnClickListener(new ImageDeleteClick(i));
}else {
//没有数据(最后一条)
tv_config_row_top_text.setVisibility(View.GONE);
iv_config_row_top_delete.setVisibility(View.GONE);
iv_config_row_top_add.setVisibility(View.GONE);
if (bundle.getInt("type") == 1){
//对比
iv_config_row_top_add.setVisibility(View.VISIBLE);
iv_config_row_top_add.setOnClickListener(this);
}
}

ll_config_content.addView(topItemView);
i++;
}


int k = 0;
for (HashMap<String, Object> titleHashMap : leftHashMaps){
//Title
View titleView = LayoutInflater.from(this).inflate(R.layout.config_table_row_item, null);
RelativeLayout rl_config_row_title = (RelativeLayout) titleView.findViewById(R.id.rl_config_row_title);
TextView tv_config_row_title_name = (TextView) titleView.findViewById(R.id.tv_config_row_title_name);

rl_config_row_title.setVisibility(View.VISIBLE);
tv_config_row_title_name.setText(titleHashMap.get("groupname").toString());

tl_config_table.addView(titleView);

JSONArray leftList = (JSONArray) titleHashMap.get("canshulist");
int j = 0;
boolean isTitle = false;
//Content
for (Object content : leftList){
View contentView = LayoutInflater.from(this).inflate(R.layout.config_table_item, null);
TextView tv_config_name = (TextView) contentView.findViewById(R.id.tv_config_name);
HScrollView hsv_config_scroll_c = (HScrollView) contentView.findViewById(R.id.hsv_config_scroll);
LinearLayout ll_config_content_c = (LinearLayout) contentView.findViewById(R.id.ll_config_content);

tv_config_name.setVisibility(View.VISIBLE);
tv_config_name.setText(content.toString());

String contentText = "";
int t = 0;
boolean isSame = false;
for (HashMap<String, Object> contentHashMap : contentHashMaps){
Log.d("count", "aaa:"+aaa + ", t:" + t + ", rankCount:" + rankCount);
aaa++;
if(rankCount == t) break;
JSONArray contentList = null;
if(contentHashMap != null){
contentList = (JSONArray) ((JSONObject)((JSONArray) contentHashMap.get("configure")).get(k)).get("canshulist");
}

//内容Text
View rowTextView = LayoutInflater.from(this).inflate(R.layout.config_table_row_item, null);
TextView tv_config_row_content_text = (TextView) rowTextView.findViewById(R.id.tv_config_row_content_text);
View v_config_row_divide = rowTextView.findViewById(R.id.v_config_row_divide);

tv_config_row_content_text.setVisibility(View.VISIBLE);
v_config_row_divide.setVisibility(View.VISIBLE);
if(contentList != null && t != (rankCount - 1)) {
String text = contentList.get(j).toString();
tv_config_row_content_text.setText(text);

if (!(boolean) tv_title.getTag()) {//判断是否要隐藏相同项
if (t == 0) contentText = text;//如果是第一个循环,记录值
if (!contentText.equals(text)){
isSame = true;
}
}else isSame = true;
}
else
tv_config_row_content_text.setText("");

ll_config_content_c.addView(rowTextView);
t++;
}
if (isSame) {
hScrollViews.add(hsv_config_scroll_c);
tl_config_table.addView(contentView);
isTitle = true;
}
j++;
}
k++;
if(!isTitle){
tl_config_table.removeView(titleView);
}
}
...全文
187 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧