开发者

Android: ListView with Unique rows

开发者 https://www.devze.com 2023-03-26 20:09 出处:网络
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)?

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 * 开发者_JS百科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;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消