2017-06-07 1 views
0

J'essaie de définir la couleur de fond lorsque l'utilisateur touche un enfant dans la liste déroulante. J'ai réussi à le faire en utilisant ce code:ExpandableListView couleur de l'arrière-plan comportement étrange

ArrayList<ChildView> childViewList; 

ExpandableListView expandablelistview_options = (ExpandableListView) view.findViewById(R.id.expandablelistview_options); 

expandablelistview_options.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

@Override 
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) 
{ 
    int index = removeChildView(groupPosition); 
    ArrayList<ConciergeOptionsPairs> children=data.Options.get(groupPosition).childrens; 

    if (index != childPosition) 
    { 
     v.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.orange)); 
     v.invalidate(); 
     addChildView(new ChildView(groupPosition, childPosition, v, children.get(childPosition))); 
    } 

    return false; 
} 
}); 

private int removeChildView(int groupPosition) 
    { 
     int index = 0; 
     int childpos = 0; 
     boolean exists = false; 
     for (ChildView c : childViewList) 
     { 
      if (c.groupPosition == groupPosition) 
      { 
       c.view.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.transparent)); 
       c.view.invalidate(); 
       childpos = c.childPosition; 
       exists = true; 
       break; 
      } 
      index++; 
     } 

     if (exists) 
     { 
      childViewList.remove(index); 
      return childpos; 
     } 

     return -1; 
    } 

private void addChildView(ChildView child) 
{ 
    childViewList.add(child); 
} 

Tout fonctionne bien, sauf de ce comportement bizarre:

J'ai 2 Vues groupe, Si j'élargissons le deuxième groupe, choisir le deuxième enfant puis développez le premier groupe, l'arrière-plan du premier enfant du premier groupe devient coloré et l'arrière-plan de l'enfant du deuxième groupe devient transparent.

enter image description here

Quand je développez le premier groupe sans choisir un enfant:

enter image description here

Si j'effondre le premier groupe, l'arrière-plan de l'enfant du deuxième groupe obtenir à nouveau de couleur.

Voici mon code ExpandableListView adaptateur:

public class ConciergeOptionsExpandableListAdapter extends BaseExpandableListAdapter 
{ 
    private Context context; 
    private ArrayList<ConciergeOptions> data; 
    private HotelStay hotelStay; 
    private int lastExpandedGroupPosition; 

    public ConciergeOptionsExpandableListAdapter(Context context, ArrayList<ConciergeOptions> data) { 
     this.context = context; 
     this.data = data; 
     hotelStay=HotelStay.getInstance(context); 
    } 

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

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

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

    @Override 
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
    { 
     ConciergeOptions options = (ConciergeOptions) getGroup(groupPosition); 

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

     RelativeLayout container = (RelativeLayout) convertView.findViewById(R.id.container); 
     TextView option_name = (TextView) convertView.findViewById(R.id.option_name); 
     ImageView arrow = (ImageView) convertView.findViewById(R.id.arrow); 

     option_name.setText(options.name); 

     if(isExpanded){ 
      convertView.setBackgroundResource(R.color.bluebutton); 
     }else{ 
      convertView.setBackgroundResource(R.color.transparent); 
     } 

     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return (this.data.get(groupPosition).childrens.size()); 
    } 


    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     ArrayList<ConciergeOptionsPairs> children=this.data.get(groupPosition).childrens; 
     if(children!=null){ 
      return children.get(childPosititon); 
     } 
     return null; 
    } 

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


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

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

     TextView option_type = (TextView) convertView.findViewById(R.id.option_type); 
     TextView price = (TextView) convertView.findViewById(R.id.price); 
     TextView decimal = (TextView) convertView.findViewById(R.id.decimal); 
     TextView currency = (TextView) convertView.findViewById(R.id.currency); 

     ConciergeOptionsPairs option_pair = (ConciergeOptionsPairs) getChild(groupPosition, childPosition); 
     option_type.setText(option_pair.name); 

     double t_price = Double.parseDouble(option_pair.value); 
     double dec = t_price - (long) t_price; 
     long decim = (long) dec; 

     int decimal_int = (int) t_price; 

     price.setText("+" + String.valueOf(decimal_int)); 
     decimal.setText("." + String.valueOf(decim)); 
     currency.setText("$"); 

     return convertView; 
    } 

    @Override 
    public void notifyDataSetChanged() { 
     super.notifyDataSetChanged(); 
    } 

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

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

Toute idée comment puis-je résoudre ce problème?

+0

donc il y aura un seul élément que vous voulez changer de couleur? – Pavan

+0

Un élément pour chaque groupe – student

+0

problème est que lorsque vous manipulez explicitement la visibilité de la couleur de l'article, etc., vous vous trompez si vous ne le manipulez pas en raison du recyclage des vues, dans votre cas, vous devez conserver le drapeau correspondant à la couleur. élément besoin de changer, vous pouvez définir ce drapeau dans votre ensemble de données et gérer même dans l'adaptateur en utilisant l'instruction conditionnelle – Pavan

Répondre

1

je considère ConciergeOptionsPairs que vos données enfant afin d'ajouter une valeur booléenne variables isSelect dans ConciergeOptionsPairs classe puis dans votre onChildClick

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

    ArrayList<ConciergeOptionsPairs> children=data.Options.get(groupPosition).childrens; //i consider this is your child view array, need to be same dataset which used as child array 
    int count=0; 
    for(ConciergeOptionsPairs objChild:children) 
    { 
     if(count==childPosition) 
     objChild.isSelect=true; 
     else 
     objChild.isSelect=false; 
    count++; 

    } 

    yourexpandableadpater.notifyDataSetChanged(); 


    return false; 
} 
}); 

maintenant dans votre adaptateur dans getChildView

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

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

      TextView option_type = (TextView) convertView.findViewById(R.id.option_type); 
      TextView price = (TextView) convertView.findViewById(R.id.price); 
      TextView decimal = (TextView) convertView.findViewById(R.id.decimal); 
      TextView currency = (TextView) convertView.findViewById(R.id.currency); 

      ConciergeOptionsPairs option_pair = (ConciergeOptionsPairs) getChild(groupPosition, childPosition); 
      option_type.setText(option_pair.name); 

      double t_price = Double.parseDouble(option_pair.value); 
      double dec = t_price - (long) t_price; 
      long decim = (long) dec; 

      int decimal_int = (int) t_price; 

      price.setText("+" + String.valueOf(decimal_int)); 
      decimal.setText("." + String.valueOf(decim)); 
      currency.setText("$"); 
      if(option_pair.isSelect) 
convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.orange)); 
    else 
convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.transparent)); 
      return convertView; 
     } 

c'est ce que je comprends votre code, vous pouvez vérifier avec ce

+1

Merci l'homme, qui a fait l'affaire! – student

+0

bienvenue, en raison du comportement de recyclage de la liste, nous devons gérer l'état des vues dans getview – Pavan

+0

Cool solution, vous êtes l'homme – student