-2

的我有四個活動課,我需要實現四個不同顏色的四個活動,我有一個像ExpandableListAdapter:如何設置顏色位置「0」 ExpandableListview

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

private Context _context; 
private List<String> _listDataHeader; // header titles 
// child data in format of header title, child title 
private HashMap<String, List<String>> _listDataChild; 
String colors; 

public ExpandableListAdapter(Context context, List<String> listDataHeader, 
     HashMap<String, List<String>> listChildData,String color) { 
    this._context = context; 
    this._listDataHeader = listDataHeader; 
    this._listDataChild = listChildData; 
    this.colors=color; 
} 

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .get(childPosititon); 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

@Override 
public View getChildView(int groupPosition, final int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 

    final String childText = (String) getChild(groupPosition, childPosition); 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_item, null); 
    } 

    CheckedTextView txtListChild = (CheckedTextView) convertView 
      .findViewById(R.id.lblListItem); 
    txtListChild.setTypeface(TypeFaceConstant.getGibsonregular(_context)); 
    txtListChild.setText(childText); 

    return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    if(groupPosition==2){ 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    }else 
     return 0; 

} 

@Override 
public Object getGroup(int groupPosition) { 
    return this._listDataHeader.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
    return this._listDataHeader.size(); 
} 

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

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 
    String headerTitle = (String) getGroup(groupPosition); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_group, null); 
    } 

    CheckedTextView lblListHeader = (CheckedTextView) convertView 
      .findViewById(R.id.lblListHeader); 
    lblListHeader.setTypeface(TypeFaceConstant.getGibsonregular(_context)); 

    lblListHeader.setText(headerTitle); 

    return convertView; 
} 

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

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 

我的活動類是像:

public class OfficeScreenActivity extends FragmentActivity implements OnClickListener { 

LinearLayout office,roaster,food_service,hospitality; 


ExpandableListAdapter listAdapter; 
ExpandableListView expListView; 
List<String> listDataHeader; 
TextView lblListHeader,office_text; 

ImageView play,logo,office_image; 
LinearLayout ln; 
Context context; 
Bundle bundle; 
ArrayList<Content> al; 

HashMap<String, List<String>> listDataChild; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_office_screen); 


    play=(ImageView) findViewById(R.id.play_button); 
    logo=(ImageView) findViewById(R.id.logo_button); 

    roaster=(LinearLayout) findViewById(R.id.roaster_lay); 
    food_service=(LinearLayout) findViewById(R.id.food_lay); 
    hospitality=(LinearLayout) findViewById(R.id.hosp_lay); 

    office_image=(ImageView) findViewById(R.id.office_image); 
    office_image.setImageResource(R.drawable.office_color); 

    office_text=(TextView) findViewById(R.id.office_text); 
    office_text.setTextColor(Color.parseColor("#57B7B2")); 



    roaster.setOnClickListener(this); 
    food_service.setOnClickListener(this); 
    hospitality.setOnClickListener(this); 
    play.setOnClickListener(this); 
    logo.setOnClickListener(this); 

    FragmentManager fm; 
    IntroductionFragment fragment = new IntroductionFragment(); 
    Bundle bundle = new Bundle(); 
    String str=" office coffiee that will \n have everyone talking."; 

    String str1="SOLA"; 
    String str2=" BEV OFFICE COFFEE SYSTEM SOLUTIONS"; 
    final String colors="#57B7B2"; 
    //bundle.putString("office",str); 
    bundle.putString("sola", str1); 
    bundle.putString("bev", str2); 
    bundle.putString("office",str); 
    bundle.putString("color", colors); 
    fragment.setArguments(bundle); 

     fm=getSupportFragmentManager(); 
     fm.beginTransaction().replace(R.id.frame1, fragment, "NewFragmentTag").commit(); 

    // get the listview 
      expListView = (ExpandableListView) findViewById(R.id.lvExp); 


      // preparing list data 
      prepareListData(); 

      listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild,colors); 

      // setting list adapter 
      expListView.setAdapter(listAdapter); 

      // Listview Group click listener 

      //expListView.setBackgroundColor(Color.parseColor("#57B7B2")); 
      expListView.setOnGroupClickListener(new OnGroupClickListener() { 
       CheckedTextView lastClickedView; 


       @Override 
       public boolean onGroupClick(ExpandableListView parent, View v, 
         int groupPosition, long id) { 


        if(groupPosition==0){ 


         FragmentManager fm; 
           IntroductionFragment fragment = new IntroductionFragment(); 
           Bundle bundle = new Bundle(); 
           String str=" office coffiee that will \n have everyone talking."; 
           String str1="SOLA"; 
           String colors="#57B7B2"; 
           String str2=" BEV OFFICE COFFEE SYSTEM SOLUTIONS"; 
           bundle.putString("office",str); 
           bundle.putString("sola", str1); 
           bundle.putString("bev", str2); 
           bundle.putString("color", colors); 
           fragment.setArguments(bundle); 

            fm=getSupportFragmentManager(); 
            fm.beginTransaction().replace(R.id.frame1, fragment, "NewFragmentTag").commit(); 


        } 
        else if(groupPosition==1){ 


         FragmentManager fm; 
           KeyPointsFragment fragment = new KeyPointsFragment(); 

           Bundle bundle = new Bundle(); 
          String token="office"; 
           bundle.putString("office", token); 
           fragment.setArguments(bundle); 

            fm=getSupportFragmentManager(); 
            fm.beginTransaction().replace(R.id.frame1, fragment, "NewFragmentTag").commit(); 

        } 
        else if(groupPosition==3){ 


         FragmentManager fm; 
           PresentationsFragment fragment = new PresentationsFragment(); 
           Bundle bundle = new Bundle(); 
           String str="PRESENTATIONS"; 
           //String str1="SOLA"; 
           String colors="#57B7B2"; 
           //String str2=" BEV OFFICE COFFEE SYSTEM SOLUTIONS"; 
           bundle.putString("present_title",str); 
           bundle.putString("cate_id", "1"); 
           //bundle.putString("sola", str1); 
           //bundle.putString("bev", str2); 
           bundle.putString("color", colors); 
           fragment.setArguments(bundle); 

            fm=getSupportFragmentManager(); 
            fm.beginTransaction().replace(R.id.frame1, fragment, "NewFragmentTag").commit(); 


        } 


        if(null!=lastClickedView){ 
          lastClickedView.setBackgroundColor(Color.parseColor("#232323")); 
         } 
         CheckedTextView txt3=(CheckedTextView) v.findViewById(R.id.lblListHeader); 
         if(!(groupPosition==2)){ 
          txt3.setBackgroundColor(Color.parseColor("#57B7B2")); 
          //txt3.setTypeface(TypeFaceConstant.getGibsonBold(getApplicationContext())); 
         lastClickedView=txt3; 
         } 

        if(groupPosition==2){ 
        lblListHeader = (TextView) v 
          .findViewById(R.id.lblListHeader); 

        lblListHeader.setTextColor(Color.parseColor("#B0AFAF")); 
        } 


        return false; 
       } 
      }); 

      // Listview Group expanded listener 
      expListView.setOnGroupExpandListener(new OnGroupExpandListener() { 

       @Override 
       public void onGroupExpand(int groupPosition) { 
        Toast.makeText(getApplicationContext(), 
          listDataHeader.get(groupPosition) + " Expanded", 
          Toast.LENGTH_SHORT).show(); 
       } 
      }); 

      // Listview Group collasped listener 
      expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() { 

       @Override 
       public void onGroupCollapse(int groupPosition) { 
        if(groupPosition==2){ 
        lblListHeader.setTextColor(Color.parseColor("#FFFFFF")); 
        } 
        Toast.makeText(getApplicationContext(), 
          listDataHeader.get(groupPosition) + " Collapsed", 
          Toast.LENGTH_SHORT).show(); 

       } 
      }); 

      // Listview on child click listener 
      expListView.setOnChildClickListener(new OnChildClickListener() { 
       CheckedTextView lastClickedView; 
       @Override 
       public boolean onChildClick(ExpandableListView parent, View v, 
         int groupPosition, int childPosition, long id) { 
        // TODO Auto-generated method stub 
        if(childPosition==0){ 


          Intent mInHome1112 = new Intent(OfficeScreenActivity.this,BrewersActivity.class); 
          mInHome1112.putExtra("cate_id", "1"); 
          OfficeScreenActivity.this.startActivity(mInHome1112); 

        } 
        else if(childPosition==1){ 

          Intent mInHome1112 = new Intent(OfficeScreenActivity.this,CapsulesActivity.class); 
          mInHome1112.putExtra("cate_id", "1"); 
          mInHome1112.putExtra("Avtivity", "Office"); 
          OfficeScreenActivity.this.startActivity(mInHome1112); 

        } 
        if(null!=lastClickedView){ 
          lastClickedView.setBackgroundColor(Color.parseColor("#232323")); 
         } 
         CheckedTextView txt1=(CheckedTextView) v.findViewById(R.id.lblListItem); 
          txt1.setBackgroundColor(Color.parseColor("#57B7B2")); 
         lastClickedView=txt1; 



        Toast.makeText(
          getApplicationContext(), 
          listDataHeader.get(groupPosition) 
            + " : " 
            + listDataChild.get(
              listDataHeader.get(groupPosition)).get(
              childPosition), Toast.LENGTH_SHORT) 
          .show(); 
        return false; 
       } 
       }); 
     } 

這是一個活動類,我有四個這樣的活動類,請幫助我。

+0

所以,你需要在每個組設置不同的顏色1日的孩子的listview,對嗎?爲此,您可以在適配器中使用'getChildView'方法並寫入如下所示的內容。 if(childPosition == 0)lblListHeader.setBackgroundColor(Color.YELLOW);其他lblListHeader.setBackgroundColor(Color.WHITE);' – varren

+0

沒有選擇任何位置,我想設置的顏色爲第一集團的地位兄弟... – PeddiRaju

回答

0

您在ExpandableListAdapter的構造函數中傳遞顏色字符串,但從未使用它。 如果要設置第一組項目的顏色,那麼你需要從getGroupView()ExpandableListAdapter的方法對其進行設置: 這樣的:

if(groupPosition==0){ 
lblListHeader.setTextColor(<color passing in constructor>); 
} 
+0

它的工作原理丹妮,但如果我點擊下一個位置它不是改變顏色巴爾克 – PeddiRaju

+0

我沒有得到你。 你能否簡單描述一下這個問題? – NilayDani

+0

請給我發郵件,我將發送PDF圖像 – PeddiRaju