4

Sou nova no Android, porem com um projeto ambicioso. Estou tentando fazer uma ExpandableList personalizada, com dois TextViews nos itens e o da esquerda precisa ter o fundo alterado conforme a satisfação do cliente.

Pedaco Layout App

Acontece que eu simplesmente não consigo colocar dois TextViews nos itens e mudar o fundo.

Alguém poderia me ajudar?

Seguem meus códigos.

Arquivo ExpandableAdapter.java:

package br.com.appcorpo.appcorpo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ExpandableAdapter extends BaseExpandableListAdapter {

    private List<String> listGroup;
    private HashMap<String, List<String>> listData;
    private LayoutInflater inflater;

    public ExpandableAdapter(Context context, List<String> listGroup, HashMap<String, List<String>> listData){
        this.listGroup = listGroup;
        this.listData = listData;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


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

    @Override
    public int getChildrenCount(int groupPosition) {
        return listData.get(listGroup.get(groupPosition)).size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return listData.get(listGroup.get(groupPosition)).get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup holderGroup;

        if(convertView == null){
            convertView = inflater.inflate(R.layout.header_expandable_list_view, null);
            holderGroup = new ViewHolderGroup();
            convertView.setTag(holderGroup);

            holderGroup.tvGroup = (TextView) convertView.findViewById(R.id.tvGroup);

        }
        else {
            holderGroup = (ViewHolderGroup)convertView.getTag();
        }

        holderGroup.tvGroup.setText(listGroup.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem holderItem;
        String val = (String) getChild(groupPosition, childPosition);

        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_expandable_list_view, null);
            holderItem = new ViewHolderItem();
            convertView.setTag(holderItem);

            holderItem.tvItem = (TextView) convertView.findViewById(R.id.tvItem);

        }
        else {
            holderItem = (ViewHolderItem)convertView.getTag();
        }

        holderItem.tvItem.setText(val);
        return convertView;
    }

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

    class ViewHolderGroup{
        TextView tvGroup;
    }

    class ViewHolderItem{
        TextView tvItem;
        TextView tvItem1;
    }
}

Arquivo FragmentCardapio.java (Parte do Fragmento que monta Expandable):

    private void expandableList(final View rootView) {
        // Tratamento do ExpandableList

        buildList();

        ExpandableListView expandableListView = (ExpandableListView) rootView.findViewById(R.id.expandableListView);
        expandableListView.setAdapter(new ExpandableAdapter(rootView.getContext(), listGroup, listData));

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                return false;
            }
        });

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
//                Toast.makeText(rootView.getContext(), "Group (Expand): " + groupPosition, Toast.LENGTH_LONG).show();

            }
        });

        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
//                Toast.makeText(rootView.getContext(), "Group (Collapse): " + groupPosition, Toast.LENGTH_LONG).show();

            }
        });
    }

    //  Metodo que monta a Listagem
    public void buildList() {

        listGroup = new ArrayList<String>();
        listData = new HashMap<String, List<String>>();
        String item;
        String aproveit;

        // Monta cabecalho do item
        for (int i = 0; i < 5; i++) {
            listGroup.add("Cafe da Manha " + (i + 1));

            //Monta item
            List<String> auxList = new ArrayList<String>();
            for (int y = 1; y < 6; y++) {
                item = "Cafe com Leite " + y;
                aproveit = "Comeu tudo";
                auxList.add(item);
                auxList.add(aproveit);
            }
            listData.put(listGroup.get(i), auxList);
        }
    }

Arquivo header_expandable_list_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/tvGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textColor="@color/azul_painel"
        android:textStyle="bold"
        android:textSize="8pt"
        android:layout_marginStart="30dp"/>

</LinearLayout>

Arquivo item_expandable_list_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/tvItem"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_weight="1"
        android:text="Aqui"
        android:textStyle="italic"
        android:textSize="7pt"/>

    <TextView
        android:id="@+id/tvItem1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="kdajfs"
        android:textStyle="italic"
        android:textSize="7pt"/>

</LinearLayout>

Obrigada.

2
  • O que acontece exatamente? Retorna algum erro?
    – Jeiferson
    Commented 2/01/2016 às 15:56
  • Oie Jeiferson, nao ocorria nenhum erro... Simplesmente nao montava a segunda coluna da parte dos itens...
    – Tatiana
    Commented 4/01/2016 às 13:37

1 Resposta 1

2

Oie gente...

Acabei conseguindo resolver meu problema. Tive que primeiramente, alterar minha variavel listData para:

    private HashMap<String, List<List<String>>> listData

e assim consegui passar para minha classe ExpandableAdapter todas as colunas.

Na classe ExpandableAdapter, alem de igualar as variaveis, eu recebi as colunas da seguinte maneira:

    String val = listData.get(listGroup.get(groupPosition)).get(childPosition).get(0);

E as alteracoes de cores foi atraves:

    holderItem.tvItem1.setBackgroundColor(Color.GREEN);

Obrigada a todos!!! :D

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .