Expandablelistview - change background color of header title group

Asked

Viewed 185 times

0

I need to change the background color of my Expandablelistview header group to a green color. Anyone have any ideas? Exemplo de onde preciso mudar

Expandablelistadapter

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    String headerTitle;
    private Context ctx;
    private List<String> listDataHeader;
    private HashMap<String, List<String>> listDataChild;
    TextView tvTitleHeader;

    public ExpandableListAdapter(Context ctx, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
        this.ctx = ctx;
        this.listDataHeader = listDataHeader;
        this.listDataChild = listChildData;
    }

    @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) {
        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>

1 answer

1


In his method getGroupView() add line to change 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"));
}
  • Half the problem has been solved haha, it changes the background, but it continues with this internal shading. Do you know how to solve this? I tried to dig around on the Internet, but I couldn’t find anything plausible

  • I think this is in your layout. Please add your list_group_vehicles.xml to the question.

  • Added! I even managed to get this shadow out, using the properties translationZ and Elevation, in the tag of Expandablelistview, but they only work from API above 21, below that I think it breaks.

  • Yeah. I’m looking at your xml here and there doesn’t seem to be anything that puts the shadow. Maybe if I take a look at your Expandablelistview tag

  • <ExpandableListView&#xA; android:id="@+id/lvExp"&#xA; android:layout_width="match_parent"&#xA; android:layout_height="wrap_content"&#xA; android:layout_marginTop="151dp"&#xA; android:groupIndicator="@null" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.