Android:具有唯一行的 ListView
是否可以创建一个 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;
}
Is it possible to create a ListView where each row in the list is a unique layout (you do not know the layout structure because it is dynamically generated at run time)?
edit:
So my rows might be something like:
- [---][-------][--][----------------]
- [-------][----------][-------------]
- [-][-------][----------][----------]
and so forth (assume the right side is aligned). Each one will most likely be unique. I know ListView assumes that the structure of the recycled view is the same as each row item but is there any way to use a Listview for dynamically created rows?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。请参阅此相关问题。它基本上说:
它有一个教程的链接以获得更多帮助。
Yes. Please see this question as it relates. It basically says:
And it has a link to a tutorial for more help.