2013-06-03 75 views
0

我正在做一個listView,我想膨脹三種不同的佈局。每行的例子都有一個條件:如果x ==「F」,那麼R.layout.row1應該膨脹,如果x ==「t」R.layout.row2應該膨脹,我有三行。使用我如何製作三種不同行佈局的ListView?

@Override 
public int getViewTypeCount() { 
    return 3; 
} 
// 
@Override 
public int getItemViewType(int position) { 

    objBean = items.get(position); 
    if (objBean.gettype() == "I") { 
     return layout2; 
    } else if (objBean.gettype() == "F") { 
     return layout1; 
    } else { 
     return layout3; 
    } 
    return i; 

} 

和getview方法

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    final ViewHolder holder; 
    objBean = items.get(position); 

    int type = getItemViewType(position); 

    String t = objBean.gettype(); 
    // if(i == 0){ 

    if (view == null) { 

     LayoutInflater inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


     if (type == layout2) { 

      view = inflater.inflate(R.layout.row1, null); 

     } else if (type == layout1) { 

      view = inflater.inflate(R.layout.list, null); 

     } else{ 
      view = inflater.inflate(R.layout.twitter, null); 
     } 
     holder = new ViewHolder(); 
     view.setTag(holder); 
    } else { 
     holder = (ViewHolder) view.getTag(); 
    } 

其中

int layout1 = 0; 
int layout2 = 1; 
int layout3 = 2; 
+0

看到這個問題1. http://stackoverflow.com/questions/11645160/listview -rows-with-different-layouts 2. http://www.vogella.com/articles/AndroidListView/article.html – SilentKiller

+0

什麼是問題? –

+0

它只返回一個視圖,即layout1 – Saad

回答

0

得到的答案試過了,問題是if語句,它不執行。 我改變

@Override 

公衆詮釋getItemViewType(INT位置){

objBean = items.get(position); 
if (objBean.gettype() == "I") { 
    return layout2; 
} else if (objBean.gettype() == "F") { 
    return layout1; 
} else { 
    return layout3; 
} 
return i; 

} 

@Override 
public int getItemViewType(int position) { 

    objBean = items.get(position); 
    String check = objBean.gettype().toString(); 
    int k = objBean.getflag(); 
    if (check.equals("F")) { 
     return layout1; 
    } else if (check.equals("T")) { 
     return layout3; 
    } else { 
     return layout2; 
    } 
} 
相關問題