2011-08-09 28 views
0

是否有可能創建一個ListView,其中列表中的每一行都是唯一的佈局(您不知道佈局結構,因爲它是在運行時動態生成的)?Android:具有唯一行的ListView

編輯:

因此,我行可能是這樣的:

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

等等(一個ssume右側對齊)。每個人最有可能是獨一無二的。我知道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; 
} 
+0

我會回答是的,你可以,但你卡在哪裏?你有一個listadapter提供你的列表視圖與行視圖? – njzk2

回答

1

是的。請參閱this問題。它基本上是說:

getViewTypeCount() - 這個方法返回多少種排 你在你的名單有信息

getItemViewType(INT位置) - 返回 你應該使用基礎的信息,其佈局類型在位置

它有一個鏈接到教程的更多幫助。

+0

這些假設你事先知道佈局的權利?現在,我不會在XML文件中創建行。每行包含大量不同長度的TextView單元格(如EPG中),並且您不知道提前的長度。 –

+0

這應該可以應用到動態創建的視圖,只要你想。基本上只需用你定製的動態視圖替換任何getViewById(id)實例。 – Jack

+0

如果每行都是唯一的,它將如何工作10000行? –