Android:具有唯一行的 ListView

发布于 2024-11-28 16:58:38 字数 2477 浏览 2 评论 0原文

是否可以创建一个 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;
}

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:

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2024-12-05 16:58:38

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

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

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

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

Yes. Please see this question as it relates. It basically says:

getViewTypeCount() - this methods returns information how many types
of rows do you have in your list

getItemViewType(int position) - returns information which layout type
you should use based on position

And it has a link to a tutorial for more help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文