【发布时间】:2011-10-23 08:46:23
【问题描述】:
是否可以创建一个 ListView,其中列表中的每一行都是一个唯一的布局(您不知道布局结构,因为它是在运行时动态生成的)?
编辑:
所以我的行可能是这样的:
- [---][-------][--][----------------]
- [--------][----------][-------------]
- [-][-------][----------][----------]
以此类推(假设右侧对齐)。每一个很可能都是独一无二的。我知道 ListView 假设回收视图的结构与每个行项相同,但是有没有办法将 Listview 用于动态创建的行?
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.guide_program_row, null);
holder = new ViewHolder();
holder.programRow = (LinearLayout)convertView.findViewById(R.id.program_row);
Log.i(TAG, "New Row; position = " + position);
}
else {
Log.i(TAG, "Cached Row; position = " + position);
holder = (ViewHolder)convertView.getTag();
}
holder.programRow.addView(buildRow(programLengths));
return convertView;
}
static class ViewHolder {
LinearLayout programRow;
}
public LinearLayout buildRow(int [] programLengthRow) {
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
int programCellWidth = screenWidth / 5;
int programCellHeight = (int)(screenHeight * 0.6 / 9);
int timeLeft = maxTimes;
LinearLayout programRow = new LinearLayout(getApplicationContext());
for (int j = 0; j < maxTimes; j++) {
if (timeLeft <= 0)
break;
Movie movie = movieList.get(j);
LinearLayout programCell = (LinearLayout)mInflater.inflate(R.layout.guide_program_cell, null);
TextView programText = (TextView)programCell.findViewById(R.id.program_cell_text);
if (programLengthRow[j] > timeLeft) {
programCell.setLayoutParams(new LinearLayout.LayoutParams(programCellWidth * timeLeft, programCellHeight));
programText.setText(movie.title + " >>");
}
else {
programCell.setLayoutParams(new LinearLayout.LayoutParams(programCellWidth * programLengthRow[j], programCellHeight));
programText.setText(movie.title);
}
timeLeft = timeLeft - programLengthRow[j];
programRow.addView(programCell);
}
return programRow;
}
【问题讨论】:
-
我会回答说是的,你可以,但是你卡在哪里了?你有一个 listadapter 为你的 listview 提供行视图吗?
标签: android listview layout row unique