ExpandableListView - change background color header title group

0

I need to change the background color of the header group from my ExpandableListView to a green color. Does anyone have any ideas?

ExpandableListAdapter

publicclassExpandableListAdapterextendsBaseExpandableListAdapter{StringheaderTitle;privateContextctx;privateList<String>listDataHeader;privateHashMap<String,List<String>>listDataChild;TextViewtvTitleHeader;publicExpandableListAdapter(Contextctx,List<String>listDataHeader,HashMap<String,List<String>>listChildData){this.ctx=ctx;this.listDataHeader=listDataHeader;this.listDataChild=listChildData;}@OverridepublicObjectgetChild(intgroupPosition,intchildPosititon){returnthis.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosititon);}@OverridepubliclonggetChildId(intgroupPosition,intchildPosition){tvTitleHeader.setText((headerTitle==null)?"Selecione" : listDataChild.get(listDataHeader.get(groupPosition)).get(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.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item_list_vehicles, null);
        }

        TextView tvItemLista = convertView.findViewById(R.id.tvw_item_nome);
        tvItemLista.setText(childText);

        return convertView;
    }

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

    @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) {
        headerTitle = (String) getGroup(groupPosition);

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

        tvTitleHeader = convertView.findViewById(R.id.tvw_list_group_title_header_model);
        tvTitleHeader.setTypeface(null, Typeface.BOLD);

        return convertView;
    }

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

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

}

MainActivity

public class HomeActivity extends BaseActivity implements HomeMVPView, NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.nav_view)
    NavigationView navigationView;

    @BindView(R.id.nav_view_menu)
    NavigationView navigationViewMenu;

    /*EXPANDABLE*/
    ExpandableListView expListView;
    ExpandableListAdapter listAdapter;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        //  PEGA A VIEW DA NAVIGATION VIEW E SETTA NUM CABECALHO, PRA DENTRO DAQUI
        View hv = navigationView.getHeaderView(0);

        ivFotoPerfil = hv.findViewById(R.id.imv_profile_home_fotoperfil);
        ivFotoCapa = hv.findViewById(R.id.imv_profile_home_fotocapa);
        tvNome = hv.findViewById(R.id.tvw_profile_home_nome);

        tvNome.setOnClickListener(v -> {
            CommonUtils.openClass(this, ProfileActivity.class);
        });

        ivFotoPerfil.setOnClickListener(v -> {
            CommonUtils.openClass(this, ProfileActivity.class);
        });

        expListView = findViewById(R.id.lvExp);

        setUpExpandable();
        showLoading();
        setUp();
        hideLoading();

        if (Build.VERSION.SDK_INT < 23) {
            //Do not need to check the permission
        } else {
            if (checkAndRequestPermissions()) {
                //If you have already permitted the permission
                Log.i("LOG", "onResume() - else [ if ]");
            }
        }
    }

    private void prepareListData() {
        listDataHeader = new ArrayList<>();
        listDataChild = new HashMap<>();

        // Adding child data
        listDataHeader.add("Lista de itens");

        // Adding child data
        List<String> itens = new ArrayList<String>();
        itens.add("Hyundai Elantra");
        itens.add("Ford Ka");
        itens.add("Chevrolet Onix");
        itens.add("Volskwagen Voyage");
        itens.add("FIAT Mobi");

        listDataChild.put(listDataHeader.get(0), itens); // Header, Child data
    }

    void setUpExpandable() {
        // preparing list data
        prepareListData();

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

        // setting list adapter
        expListView.setAdapter(listAdapter);
        expListView.setFooterDividersEnabled(false);
        expListView.setHeaderDividersEnabled(false);
        expListView.setDividerHeight(0);
        //  PEGA A POSIÇÃO DO ITEM DA LISTA
        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Log.i("LOG", "Posição: " + listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition));
                parent.collapseGroup(0);

                return false;
            }

        });
    }

}

list_group_vehicles.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#F5F5F5"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingBottom="8dp"
    android:paddingEnd="8dp"
    android:paddingStart="16dp"
    android:paddingTop="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvw_list_group_title_header_model"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hyundai Elantra"
            android:textColor="#242424"
            android:textSize="12dp"
            tools:text="Hyundai i30" />

        <TextView
            android:id="@+id/tvw_list_group_title_header_plate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:text="UFD-3495"
            android:textColor="#242424"
            android:textSize="12dp"
            tools:text="UFD-3495" />

    </LinearLayout>

    <ImageView
        android:id="@+id/ivw_list_group_title_header_arrow"
        android:layout_width="14dp"
        android:layout_height="14dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="16dp"
        android:src="@drawable/arrow_down" />

</RelativeLayout>
    
asked by anonymous 26.02.2018 / 15:41

1 answer

1

In your method getGroupView() add the line to change the background color:

if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group_vehicles, null);
            convertView.setBackgroundColor(Color.parseColor("#00FF00"));
}
    
26.02.2018 / 18:30