如何为ExpandableListview的位置“0”设置颜色

时间:2015-06-20 06:34:17

标签: android expandablelistview background-color expandablelistadapter

我有四个活动类,我需要为四个活动实现四种不同的颜色,我有一个ExpandableListAdapter,如:

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;
                }
                });
        }

这是活动类之一,我有四个这样的活动类,请帮助我。

1 个答案:

答案 0 :(得分:0)

您在ExpandableListAdapter的构造函数中传递颜色String但从未使用它。 如果要设置第一个组项的颜色,则需要从ExpandableListAdapter的getGroupView()方法设置它: 像这样:

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