Expandablelist Customizable

Asked

Viewed 49 times

4

I’m new to Android, but with an ambitious project. I’m trying to make a ExpandableList customized, with two TextViews on the items and the one on the left needs to have the background changed as per customer satisfaction.

Pedaco Layout App

Turns out I just can’t put two TextViews in the items and change the background.

Could someone help me?

Follow my codes.

Filing cabinet 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;
    }
}

Filing cabinet FragmentCardapio.java (Part of the Mounting Fragment 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);
        }
    }

Filing cabinet 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>

Filing cabinet 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>

Thank you.

  • What exactly happens? Returns some error?

  • Oie Jeiferson, no error occurred... Simply did not mount the second column of the part of items...

1 answer

2

Look at the people...

I managed to solve my problem. I had to first change my list variableData to:

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

and thus I was able to pass to my class Expandabled all the columns.

In the class Expandableadapter, besides equalizing the variables, I received the columns as follows:

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

And changes of colours were:

    holderItem.tvItem1.setBackgroundColor(Color.GREEN);

Thank you all!!! D

Browser other questions tagged

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