Expandable ListView Example
This example will show you how can you work with an expandable list in the android application.
Steps:-
1) Create two layout XML file.(One for header(parent) and Second for item list(child)).
2) Create one new java file for an expandable adapter.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myexpandablelist">
</ExpandableListView>
</RelativeLayout>
header.xml
<LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2" />
</LinearLayout>
child.xml
<LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" />
</LinearLayout>
Main.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.myexpandablelist);
ArrayList<String> myArrayHeader=new ArrayList<>();
HashMap<String,ArrayList<String>> myHashChild=new HashMap<>();
//Add data to ArrayList
myArrayHeader.add("Superman Vs Batman");
myArrayHeader.add("The Wolverine");
//Add data to HashMap
myHashChild.put("Movies", myArrayHeader);
//Add data to ArrayList
myArrayHeader=new ArrayList<>();
myArrayHeader.add("I'll Be Your Man");
myArrayHeader.add("I Can't Give Everything Away");
//Add data to HashMap
myHashChild.put("Music", myArrayHeader);
//Add data to ArrayList
myArrayHeader=new ArrayList<>();
myArrayHeader.add("Breaking Bad");
myArrayHeader.add("Add to WatchlistGame of Thrones");
//Add data to HashMap
myHashChild.put("Serials", myArrayHeader);
ArrayList<String> myArrayTitle=new ArrayList<>();
myArrayTitle.add("Movies");
myArrayTitle.add("Music");
myArrayTitle.add("Serials");
expandableListView.setAdapter(new MyRecylerAdapter(this, myHashChild, myArrayTitle));
}}
Myadapter.java
BaseExpandableListAdapter {
HashMap<String,ArrayList<String>> myHashChild;
Context context;
ArrayList<String> myArrayTitle;
public MyRecylerAdapter(MainActivity mainActivity, HashMap<String, ArrayList<String>> myHashChild, ArrayList<String> myArrayTitle) {
this.myHashChild=myHashChild;
this.myArrayTitle=myArrayTitle;
context=mainActivity;
}
@Override
public int getGroupCount() { return myHashChild.size();}
@Override
public int getChildrenCount(int groupPosition) {
return myHashChild.get(myArrayTitle.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {return myHashChild.get(groupPosition);}
@Override
public Object getChild(int groupPosition, int childPosition) {
return myHashChild.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) { return 0;}
@Override
public long getChildId(int groupPosition, int childPosition) { return 0; }
@Override
public boolean hasStableIds() { return false;}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.header, null);
TextView myText=(TextView) convertView.findViewById(R.id.textView2);
myText.setText(myArrayTitle.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child, null);
TextView myText=(TextView) convertView.findViewById(R.id.textView);
myText.setText(myHashChild.get(myArrayTitle.get(groupPosition)).get(childPosition));
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
Output
Steps:-
1) Create two layout XML file.(One for header(parent) and Second for item list(child)).
2) Create one new java file for an expandable adapter.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myexpandablelist">
</ExpandableListView>
</RelativeLayout>
header.xml
<LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2" />
</LinearLayout>
child.xml
<LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" />
</LinearLayout>
Main.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.myexpandablelist);
ArrayList<String> myArrayHeader=new ArrayList<>();
HashMap<String,ArrayList<String>> myHashChild=new HashMap<>();
//Add data to ArrayList
myArrayHeader.add("Superman Vs Batman");
myArrayHeader.add("The Wolverine");
//Add data to HashMap
myHashChild.put("Movies", myArrayHeader);
//Add data to ArrayList
myArrayHeader=new ArrayList<>();
myArrayHeader.add("I'll Be Your Man");
myArrayHeader.add("I Can't Give Everything Away");
//Add data to HashMap
myHashChild.put("Music", myArrayHeader);
//Add data to ArrayList
myArrayHeader=new ArrayList<>();
myArrayHeader.add("Breaking Bad");
myArrayHeader.add("Add to WatchlistGame of Thrones");
//Add data to HashMap
myHashChild.put("Serials", myArrayHeader);
ArrayList<String> myArrayTitle=new ArrayList<>();
myArrayTitle.add("Movies");
myArrayTitle.add("Music");
myArrayTitle.add("Serials");
expandableListView.setAdapter(new MyRecylerAdapter(this, myHashChild, myArrayTitle));
}}
Myadapter.java
BaseExpandableListAdapter {
HashMap<String,ArrayList<String>> myHashChild;
Context context;
ArrayList<String> myArrayTitle;
public MyRecylerAdapter(MainActivity mainActivity, HashMap<String, ArrayList<String>> myHashChild, ArrayList<String> myArrayTitle) {
this.myHashChild=myHashChild;
this.myArrayTitle=myArrayTitle;
context=mainActivity;
}
@Override
public int getGroupCount() { return myHashChild.size();}
@Override
public int getChildrenCount(int groupPosition) {
return myHashChild.get(myArrayTitle.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {return myHashChild.get(groupPosition);}
@Override
public Object getChild(int groupPosition, int childPosition) {
return myHashChild.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) { return 0;}
@Override
public long getChildId(int groupPosition, int childPosition) { return 0; }
@Override
public boolean hasStableIds() { return false;}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.header, null);
TextView myText=(TextView) convertView.findViewById(R.id.textView2);
myText.setText(myArrayTitle.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child, null);
TextView myText=(TextView) convertView.findViewById(R.id.textView);
myText.setText(myHashChild.get(myArrayTitle.get(groupPosition)).get(childPosition));
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
Output

Comments
Post a Comment