ExpandableListView

ExpandableListView viene utilizzata per raggruppare i dati dell’elenco per categorie. Quindi  espande e comprime i gruppi per mostrare o nascondere i suoi elementi.

Andiamo a creare l’interfaccia  utente della visualizzazione elenco espandibile:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:orientation=”vertical”>

<ExpandableListView
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:id=”@+id/expandableListView”
android:textAppearance=”?android:attr/textAppearanceMedium”/>

</LinearLayout>

Andiamo a creare il file  list_group.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical”>

<TextView
android:id=”@+id/header”
android:layout_width=”match_parent”
android:layout_height=”100dp”
android:textSize=”30sp”/>

</LinearLayout>

Per gli elementi secondari andiamo a creare il file list_items.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical”>

<TextView
android:id=”@+id/child”
android:layout_width=”match_parent”
android:layout_height=”30dp”
android:textSize=”20sp”/>

</LinearLayout>

Adesso andiamo a creare la classe MyListAdapter.java che si estende dalla classe BaseExpandableListAdapter :

package com.example.expandableListView.activities;
import android.content.*;
import android.widget.*;
import java.util.*;
import android.view.*;
import com.example.expandableListView.*;

public class MyListAdapter extends BaseExpandableListAdapter
{

private final Context context;
private final HashMap<String,List<String>> child;
private final List<String> header;

public MyListAdapter(Context context, HashMap<String, List<String>> child, List<String> header) {
this.context = context;
this.child = child;
this.header =header;


}

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



@Override
public int getChildrenCount(int groupPos) {
return child.get(header.get(groupPos)).size();
}

@Override
public Object getGroup(int groupPos) {
return header.get(groupPos);
}

@Override
public Object getChild(int groupPos, int childPos) {
return child.get(header.get(groupPos)).get(childPos);
}


@Override
public long getChildId(int groupPos, int childPos) {
return childPos;
}

@Override
public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView, ViewGroup parent) {

String childTitle = (String) getChild(groupPos, childPos);

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = inflater.inflate(R.layout.list_items, null);

}

TextView textView = convertView.findViewById(R.id.child);
textView.setText(childTitle);

return convertView;
}

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

@Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent) {

String headerTitle = (String) getGroup(groupPos);

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = inflater.inflate(R.layout.list_group, null);

}

TextView textView = convertView.findViewById(R.id.header);
textView.setText(headerTitle);

return convertView;

}

  @Override
public boolean isChildSelectable(int groupPos, int childPos) {
return true;
}
  @Override
public boolean hasStableIds() {
return false;
}

In fine MainActivity.java dove impostiamo gli elementi su adattatore per recuperare i dati da visualizzare nell’ elenco:

package com.example.expandableListView.activities;

import android.app.Activity;
import android.os.Bundle;
import com.example.expandableListView.R;
import java.util.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity {
HashMap<String, List<String>> child;
    List<String> header;
   
ExpandableListView expandableListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

expandableListView= findViewById(R.id.expandableListView);

       
list();

       MyListAdapter listAdapter = new MyListAdapter(this, child, header);

      
        expandableListView.setAdapter(listAdapter);

       
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPos, int childPos, long id) {

return false;
}
});

}

    

   private void list() {

   header = new ArrayList<String>();
   child = new HashMap<String, List<String>>(); 

        header.add(“Primary colors”);
        header.add(“Secondary colors”);
   header.add(“Tertiary colors”);

       
        List<String> header1 = new ArrayList<String>();
        header1.add(“Red”);
        header1.add(“Blue”);
        header1.add(“Yellow”);
       

        List<String> header2 = new ArrayList<String>();
        header2.add(“Orange”);
        header2.add(“Green”);
        header2.add(“Purple”);
       

        List<String> header3 = new ArrayList<String>();
   header3.add(“Lime”);
   header3.add(“Amber”);
   header3.add(“Vermilion”);
   header3.add(“Garnet”);
   header3.add(“Turquoise”);
   header3.add(“Violet”);

       child.put(header.get(0), header1);
   child.put(header.get(1), header2);
   child.put(header.get(2), header3);

    }

}