2013-05-26 5 views
1

확장 가능한보기에서 각기 다른 배경색을 개별 그룹 이름으로 설정할 수 있습니까?Android 확장형 목록 개별 그룹에 배경색 설정

I 그룹 이름으로 그룹을 생성 코드가 - 그룹 (10) 각 그룹에 그룹 1은 아이가 - 자녀 1 자녀 3

에 내가 따라 서로 다른 배경의 각 그룹에 색상과 아이를주고 싶어 그룹 이름에. 조건이 충족되지 않으면 기본값입니다.
예 :
그룹 1 - 화이트 배경
그룹 2 - 빨간색 배경
그룹 3 - 회색 배경
그룹 4 - 노란색 배경

main.xml에 : 확장 목록 레이아웃

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

     <ExpandableListView 
      android:id="@+id/expandable_list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

     <ListView 
      android:id="@+id/android:list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:choiceMode="multipleChoice" /> 

</LinearLayout> 

grouprow.xml : 그룹 텍스트 레이아웃

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

    <TextView android:id="@+id/rowname" 
     android:paddingLeft="50px" 
     android:textSize="30px" 
     android:textColor="@drawable/blue" 
     android:textStyle="normal" 
     android:layout_width="fill_parent" 
     android:layout_height="50px"/> 

</LinearLayout> 

childrow.xml : 자식 텍스트 레이아웃

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

    <TextView android:id="@+id/grpchild" 
     android:paddingLeft="50px" 
     android:focusable="false" 
     android:textSize="14px" 
     android:textStyle="normal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

상위 그룹 클래스 : 클래스는 각 상위 항목을 만들 수

public class CategoryGroup { 
     private String mTitle; 
     private ArrayList<String> mArrayChildren; 

     public String getTitle() { 
      return mTitle; 
     } 

     public void setTitle(String mTitle) { 
      this.mTitle = mTitle; 
     } 

     public ArrayList<String> getArrayChildren() { 
      return mArrayChildren; 
     } 

     public void setArrayChildren(ArrayList<String> mArrayChildren) { 
      this.mArrayChildren = new ArrayList(mArrayChildren); 
     } 
} 

확장리스트 클래스 :

public class ExpandableListItems extends BaseExpandableListAdapter { 

      private LayoutInflater inflater; 
      private ArrayList<CategoryGroup> mParent; 

      public ExpandableListItems(Context context, ArrayList<CategoryGroup> parent) { 
       mParent = parent; 
       inflater = LayoutInflater.from(context); 
      } 


      @Override 
      //counts the number of group/parent items so the list knows how many times calls getGroupView() method 
      public int getGroupCount() { 
       return mParent.size(); 
      } 

      @Override 
      //counts the number of children items so the list knows how many times calls getChildView() method 
      public int getChildrenCount(int i) { 
       return mParent.get(i).getArrayChildren().size(); 
      } 

      @Override 
      //gets the title of each parent/group 
      public Object getGroup(int i) { 
       return mParent.get(i).getTitle(); 
      } 

      @Override 
      //gets the name of each item 
      public Object getChild(int i, int i1) { 
       return mParent.get(i).getArrayChildren().get(i1); 
      } 

      @Override 
      public long getGroupId(int i) { 
       return i; 
      } 

      @Override 
      public long getChildId(int i, int i1) { 
       return i1; 
      } 

      @Override 
      public boolean hasStableIds() { 
       return true; 
      } 

      @Override 
      //in this method you must set the text to see the parent/group on the list 
      public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { 

       if (view == null) { 
        System.out.println("i: " + i + "; b: " + b); 
        view = inflater.inflate(R.layout.grouprow, viewGroup, false); 
       } 


       TextView textView = (TextView) view.findViewById(R.id.rowname); 

       //"i" is the position of the parent/group in the list 

       textView.setText(getGroup(i).toString()); 

       //return the entire view 
       return view; 
      } 

      @Override 
      //in this method you must set the text to see the children on the list 
      public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) { 
       if (view == null) { 
        view = inflater.inflate(R.layout.childrow, viewGroup, false); 
       } 

       TextView textView = (TextView) view.findViewById(R.id.grpchild); 
       //"i" is the position of the parent/group in the list and 
       //"i1" is the position of the child 
       textView.setText(mParent.get(i).getArrayChildren().get(i1)); 

       //return the entire view 
       return view; 
      } 

      @Override 
      public boolean isChildSelectable(int i, int i1) { 
       return true; 
      } 

      @Override 
      public void registerDataSetObserver(DataSetObserver observer) { 
       /* used to make the notifyDataSetChanged() method work */ 
       super.registerDataSetObserver(observer); 
      } 
} 
01 23,516,

주요 활동 클래스 :이 작업을 수행 할 수

public class GetResourcesListActivity extends ListActivity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     private ExpandableListView mExpandableList; 
     private ExpandableListItems mAdapter; 
     ArrayList<CategoryGroup> arrayParentsGroups = new ArrayList<CategoryGroup>(); 

     mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list); 
     arrayParents = createGroupList(); 

     mAdapter = new ExpandableListItems(this, arrayParentsGroups); 
     mExpandableList.setAdapter(mAdapter); 

     private List createGroupList() { 
      ArrayList arrayParents = new ArrayList(); 
      for(int i = 0 ; i < 10 ; ++i) { // 10 groups........ 
       CategoryGroup parent = new CategoryGroup(); 
       parent.setTitle("Group" + i); 

       ArrayList<String> arrayChildren = new ArrayList<String>(); 
       for (int j = 0; j < 3; j++) { 
        arrayChildren.add("Child " + j); 
       } 
       parent.setArrayChildren(arrayChildren); 
       arrayParents.add(parent); 
      } 
      return arrayParents; 
     } 
    } 
) 

답변

1

쉬운 방법은 다음과 같다.

CategoryGroup 개체에 int mBackgroundRes이라는 또 다른 속성을 지정하십시오. 또한 해당 getter 및 setter 메서드를 추가합니다. 당신의 BaseExpandableListAdapter 변화,

private ArrayList<CategoryGroup> createGroupList() { 
    ArrayList arrayParents = new ArrayList(); 
    for (int i = 0; i < 10; ++i) { // 10 groups........ 
     CategoryGroup parent = new CategoryGroup(); 
     parent.setTitle("Group" + i); 

     int background = android.R.color.black; 
     if (i == 0) { 
      background = android.R.color.white; 
     } else if (i == 1) { 
      background = android.R.color.holo_red_light; 
     } else if (i == 2) { 
      background = android.R.color.darker_gray; 
     } 
     parent.setBackgroundRes(background); 

     ArrayList<String> arrayChildren = new ArrayList<String>(); 
     for (int j = 0; j < 3; j++) { 
      arrayChildren.add("Child " + j); 
     } 
     parent.setArrayChildren(arrayChildren); 
     arrayParents.add(parent); 
    } 
    return arrayParents; 
} 

마지막으로 올바른 개체 유형을 반환하는 getGroup() 방법 : 당신의 createGroupList() 방법에서

당신은 당신의 논리에 따라 배경 리소스를 설정합니다.

@Override 
public CategoryGroup getGroup(int i) { 
    return mParent.get(i); 
} 

그리고 getGroupView()getChildView() 방법 모두 각각의 뷰에 대한 배경 설정 :

@Override 
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { 

    if (view == null) { 
     System.out.println("i: " + i + "; b: " + b); 
     view = inflater.inflate(R.layout.grouprow, viewGroup, false); 
    } 

    view.setBackgroundResource(getGroup(i).getBackgroundRes()); 

    TextView textView = (TextView) view.findViewById(R.id.rowname); 
    textView.setText(getGroup(i).getTitle()); 

    return view; 
} 

@Override 
public View getChildView(int i, int i1, boolean b, View view, 
     ViewGroup viewGroup) { 

    if (view == null) { 
     view = inflater.inflate(R.layout.childrow, viewGroup, false); 
    } 

    view.setBackgroundResource(getGroup(i).getBackgroundRes()); 

    TextView textView = (TextView) view.findViewById(R.id.grpchild); 
    textView.setText(mParent.get(i).getArrayChildren().get(i1)); 

    return view; 
} 
+0

감사합니다. JensIts는 훌륭하게 작동합니다. 배경뿐만 아니라 각 그룹 행의 텍스트 옆에 이미지를 추가 할 수 있습니까? (아이들에게는 아님). 각 그룹 행마다 다른 이미지가 있습니다. – Kranthi

0

업데이트 이런 일에 어댑터 :

@Override 
    //in this method you must set the text to see the parent/group on the list 
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { 

      if (view == null) { 
       System.out.println("i: " + i + "; b: " + b); 
       view = inflater.inflate(R.layout.grouprow, viewGroup, false); 
       switch(i){ // i is the groupPosition - 0-based indexing 
       case 0: view.setBackgroundColor(android.R.color.white); break; 
       case 1: view.setBackgroundColor(android.R.color.holo_dark_red); break; 
       ... 
       } 
      } 

      TextView textView = (TextView) view.findViewById(R.id.rowname); 
      //"i" is the position of the parent/group in the list 
      textView.setText(getGroup(i).toString()); 
      //return the entire view 
      return view; 
    } 
을 당신은 간단한 변경 Object CategoryGroup에 수

제공된 색상이 충분하지 않으면 (android.R.color.xxx) 나만의 색상을 만들 수 있습니다. 가 입술에 새 파일을 확인/colors.xml 후 자신의 색상 정의라는 이름의 값 : 다음

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<color name="white">#FFFFFF</color> 
<color name="yellow">#FFFF00</color> 
<color name="red">#FF0000</color> 
<color name="gray">#808080</color> 
<color name="purple">#800080</color> 
<color name="green">#008000</color> 
<color name="blue">#0000FF</color> 
<color name="black">#000000</color> 
</resources> 

이처럼 코드에서 참조 할 수 있습니다 : view.setBackgroundColor(R.color.blue);

편집 : 경우의 이 방법으로 작동하지 않는 경우 해결 된 색상으로 시도하십시오. view.setBackgroundColor(getResources().getColor(android.R.color.white));

방금 ​​내 자신의 ExpandableListView에 붙여 넣었을 때 문제가 없습니다.

+0

감사합니다. Matt. 나는 그것을 똑같이 시도했다. 배경은 설정되어 있지 않습니다. – Kranthi