I'm new to Android, but with an ambitious project.
I'm trying to make a% custom% with two ExpandableList
on items and the one on the left needs to have the background changed as per customer satisfaction.
ItturnsoutthatIjustcannotseemtoputtwoTextViews
ontheitemsandchangethebackground.
Couldanyonehelpme?
Followmycodes.
FileTextViews
:
packagebr.com.appcorpo.appcorpo;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseExpandableListAdapter;importandroid.widget.TextView;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;publicclassExpandableAdapterextendsBaseExpandableListAdapter{privateList<String>listGroup;privateHashMap<String,List<String>>listData;privateLayoutInflaterinflater;publicExpandableAdapter(Contextcontext,List<String>listGroup,HashMap<String,List<String>>listData){this.listGroup=listGroup;this.listData=listData;inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@OverridepublicintgetGroupCount(){returnlistGroup.size();}@OverridepublicintgetChildrenCount(intgroupPosition){returnlistData.get(listGroup.get(groupPosition)).size();}@OverridepublicObjectgetGroup(intgroupPosition){returnlistGroup.get(groupPosition);}@OverridepublicObjectgetChild(intgroupPosition,intchildPosition){returnlistData.get(listGroup.get(groupPosition)).get(childPosition);}@OverridepubliclonggetGroupId(intgroupPosition){returngroupPosition;}@OverridepubliclonggetChildId(intgroupPosition,intchildPosition){returnchildPosition;}@OverridepublicbooleanhasStableIds(){returnfalse;}@OverridepublicViewgetGroupView(intgroupPosition,booleanisExpanded,ViewconvertView,ViewGroupparent){ViewHolderGroupholderGroup;if(convertView==null){convertView=inflater.inflate(R.layout.header_expandable_list_view,null);holderGroup=newViewHolderGroup();convertView.setTag(holderGroup);holderGroup.tvGroup=(TextView)convertView.findViewById(R.id.tvGroup);}else{holderGroup=(ViewHolderGroup)convertView.getTag();}holderGroup.tvGroup.setText(listGroup.get(groupPosition));returnconvertView;}@OverridepublicViewgetChildView(intgroupPosition,intchildPosition,booleanisLastChild,ViewconvertView,ViewGroupparent){ViewHolderItemholderItem;Stringval=(String)getChild(groupPosition,childPosition);if(convertView==null){convertView=inflater.inflate(R.layout.item_expandable_list_view,null);holderItem=newViewHolderItem();convertView.setTag(holderItem);holderItem.tvItem=(TextView)convertView.findViewById(R.id.tvItem);}else{holderItem=(ViewHolderItem)convertView.getTag();}holderItem.tvItem.setText(val);returnconvertView;}@OverridepublicbooleanisChildSelectable(intgroupPosition,intchildPosition){returnfalse;}classViewHolderGroup{TextViewtvGroup;}classViewHolderItem{TextViewtvItem;TextViewtvItem1;}}
FileExpandableAdapter.java
(FragmentpartthatmountsFragmentCardapio.java
):
privatevoidexpandableList(finalViewrootView){//TratamentodoExpandableListbuildList();ExpandableListViewexpandableListView=(ExpandableListView)rootView.findViewById(R.id.expandableListView);expandableListView.setAdapter(newExpandableAdapter(rootView.getContext(),listGroup,listData));expandableListView.setOnChildClickListener(newExpandableListView.OnChildClickListener(){@OverridepublicbooleanonChildClick(ExpandableListViewparent,Viewv,intgroupPosition,intchildPosition,longid){returnfalse;}});expandableListView.setOnGroupExpandListener(newExpandableListView.OnGroupExpandListener(){@OverridepublicvoidonGroupExpand(intgroupPosition){//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);
}
}
File Expandable
:
<?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>
File header_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.