2014-01-23 53 views
0

在我的應用程序中,我有一個ExpandableListViewBaseExpandableListAdapter。該方法getChildView樣子:ListViewAdapter不同風格的行

@Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     MyCustomObject myObject = (MyCustomObject)getGroup(groupPosition)).getValue().get(childPosition); 
     if(convertView==null){ 
      LayoutInflater inflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.list_item, null); 
     } 
     TextView lbl = (TextView) convertView.findViewById(R.id.lbl); 
     lbl.setText(myObject.getText()); 

     return convertView; 
    } 

我想改變這取決於對myObject財產佈局的背景(和其他功能)。我的第一個嘗試是定義不同的風格,並嘗試動態改變視圖的風格,但這是不可能的。

什麼是最好和最有效的方法來實現這一目標?

謝謝!

+0

除了背景之外,你還想改變什麼? – nKn

+0

我還不確定,但讓我說我想添加一個圖標(或根據屬性更改其可見性)。 – mario595

回答

1

你可以動態地做到這一點,沒有任何問題。非常非正式地說,您的convertView實例中有整行佈局。如果您想更改背景,請致電convertView.setBackground(...)。如果您的佈局中包含更多元素,只需使用convertView.findElementById(R.id...)即可獲得它們,然後使其成爲您所需的更改。

0

getChildView()給你groupPositionchildPosition。爲了確定哪一個是當前的「子女View」,您需要使用這兩個參數(即switchgroupPosition並創建與您擁有的組數量一樣多的個案)。在每種情況下,您都會膨脹並返回相應的行View,並使用myObject來設置要在行的相應子區中顯示的任何值。

0

這是我通常做:

首先做一個list_item.xml文件:

<?xml version="1.0" encoding="utf-8" ?> 
    <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:orientation="horizontal"> 

    <LinearLayout android:id="@+id/colorBar" 
      android:layout_height="match_parent" 
      android:layout_width="32dp" 
      android:orientation="vertical"/> 
    <TextView 
      android:id="@+id/mytextView1" 
      android:layout_marginLeft="32dp" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent"/> 

    </LinearLayout> 

然後在你的BaseExpandableListAdapter創建類:

public static class Row{ 
     LinearLayout m_llColorBar; 
     TextView m_myTextView; 

    } 

而在做到這一點你getChildView()方法

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
{ 
    // Row Class reference 
    Row curRow; 

    // Reference to your custom object Parent 
    MyCustomObject myObject = (MyCustomObject)getGroup(groupPosition)).getValue().get(childPosition); 

    // Context should be passed into Constructor 
    LayoutInflater inflater = context.getLayoutInflater(); 

    // If convertView is null, we need to create a new Child Layout 
    if(convertView == null) 
    { 
     curRow = new Row(); 
     convertView = inflater.inflate(R.layout.list_item, null); 
     curRow.m_llColorBar = (LinearLayout) convertView.findViewById(R.id.colorBar); 
     curRow.m_myTextView = (TextView) convertView.findViewById(R.id.myTextView1); 

     // Sets a unique tag for the current row 
     convertView.setTag(curRow); 

    // Else, 
    }else{ 

     // Recycle the view 
     curRow = (Row) convertView.getTag(); 
    } 

    // Set the Text View to the Object's Text 
    curRow.m_myTextView.setText(myObject.getText()); 

    // If the Object's Text is Some value 
    if(myObject.getText().equals("Some Value")){ 

     // Make the Bar the Dark Holo Blue 
     curRow.m_llColorBar.setBackground(context.getResources().getColor(
       android.R.color.holo_blue_dark)); 

    }else if(myObject.getText().equals("Some Other Value")){ 

     // Make the Bar the Dark Holo Orange 
     curRow.m_llColorBar.setBackground(context.getResources().getColor(
       android.R.color.holo_orange_dark)); 
    }else{ 

     // Make the Bar the Dark Holo Red 
     curRow.m_llColorBar.setBackground(context.getResources().getColor(
       android.R.color.holo_red_dark)); 

    } 

    // Return the View 
    return convertView; 
} 

這說明如何使基於內容的行項目看起來不同。

  1. 只是一個簡單的類牽着你的UI元素
  2. 參考這個在您的getChildView()
  3. 初始化它,如果視圖是空
  4. 使用視圖,讓您的項目
  5. 然後對在你的getChildView()方法中循環。

而且你可以用任何的Widget這樣做,所以說你有,你可能希望基於這些內容你的行中包括ImageView和多張圖片。只需將ImageView引用添加到簡單的Class行,然後使用if語句根據對象中的文本動態添加新的ImageView src。