【问题标题】:Android: ListView with Unique rowsAndroid:具有唯一行的 ListView
【发布时间】:2011-10-23 08:46:23
【问题描述】:

是否可以创建一个 ListView,其中列表中的每一行都是一个唯一的布局(您不知道布局结构,因为它是在运行时动态生成的)?

编辑:

所以我的行可能是这样的:

  1. [---][-------][--][----------------]
  2. [--------][----------][-------------]
  3. [-][-------][----------][----------]

以此类推(假设右侧对齐)。每一个很可能都是独一无二的。我知道 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


【解决方案1】:

是的。请参阅相关的this 问题。它基本上说:

getViewTypeCount() - 这个方法返回信息有多少类型 你的列表中有多少行

getItemViewType(int position) - 返回布局类型的信息 你应该根据位置使用

它有一个教程链接以获得更多帮助。

【讨论】:

  • 这些假设您事先知道布局,对吧?现在,我不在 XML 文件中创建行。每行都包含大量不同长度的 TextView 单元格(例如在 EPG 中),而您事先不知道长度。
  • 这应该可以根据需要应用于动态创建的视图。基本上只需用您自定义构建的动态视图替换任何 getViewById(id) 实例。
  • 此外,每一行都是一个线性布局,线性布局内部是可变数量的不同长度的 TextView(请参阅原始问题中的编辑)。
  • 你能更准确地描述你想要完成的事情吗?根据某些条件,您是否只需要每行有 1、2 或 3 个项目?
  • 我想我的下一个问题是......你的代码具体有什么问题?看起来你正在做的事情会很好。
猜你喜欢
  • 1970-01-01
  • 2012-04-30
  • 1970-01-01
  • 2012-07-02
  • 2013-11-12
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
相关资源
最近更新 更多