How can i disable Expandable Listview icon from it's header in android?

后端 未结 2 1168
南笙
南笙 2021-01-28 21:03

I created an ExpandableListView in android. I have some categories which have some more sub-categories. While other do not have sub-categories. How can I remove the

相关标签:
2条回答
  • 2021-01-28 21:18

    There is single problem in above answer. While you expand the category other category's icon was also disable. Because, at the first time code goes to try and it's not goes outside of try until your listdatachild.get(listdataheader.get(i)).get(0) found. so, it not going to in if(check) everytime. Therefor, icon were remaing hidden.

    Below is the perfect solution

    @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.YOUR_LIST_GROUP_XML, null);
        }
    
        TextView header_tv = (TextView) convertView.findViewById(R.id.list_header_tv);
        ImageView arrow_iv =(ImageView) convertView.findViewById(R.id.arrow_iv);
    
    
        header_tv.setText(headerTitle);
    
        Boolean check = false;
    
        try {
    
    
            if(YOUR_HAS_MAP_LIST.get(this._listDataHeader.get(groupPosition))
                    .get(0)==null){}
            else{
                 if (isExpanded) {
                    arrow_iv.setImageResource(R.drawable.arrow_up);
                } else {
                    arrow_iv.setImageResource(R.drawable.arrow_down);
                }
             }
        }
        catch (Exception ex)
        {
            check = true;
            ex.printStackTrace();
        }
        if(check)
        {
            arrow_iv.setVisibility(View.INVISIBLE);
        }
        else{
            arrow_iv.setVisibility(View.VISIBLE);
        }
        return convertView;
    }
    
    0 讨论(0)
  • 2021-01-28 21:29

    First of all , remove the default icon of expandable listview

    <ExpandableListView [...] 
        android:groupIndicator="@null" />
    

    And than in your adapter of Expandable listview in getGroupView method use following code for it

    @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.YOUR_LIST_GROUP_XML, null);
        }
    
        TextView header_tv = (TextView) convertView.findViewById(R.id.list_header_tv);
        ImageView arrow_iv =(ImageView) convertView.findViewById(R.id.arrow_iv);
    
    
        header_tv.setText(headerTitle);
    
        Boolean check = false;
    
        try {
    
    
            YOUR_HAS_MAP_LIST.get(this._listDataHeader.get(groupPosition))
                    .get(0);
        }
        catch (Exception ex)
        {
            check = true;
            ex.printStackTrace();
        }
        if(check)
        {
            arrow_iv.setVisibility(View.INVISIBLE);
        }
    
        if(isExpanded)
        {
            arrow_iv.setImageResource(R.drawable.UP_ARROW);
        }
        else
        {
            arrow_iv.setImageResource(R.drawable.DOWN_ARROW);
        }
    
        return convertView;
    }
    
    0 讨论(0)
提交回复
热议问题