Created
January 15, 2017 17:36
-
-
Save farooqkhan003/82297e1b10b098ce8f4b009502941a9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.demo.farooq.sublistlistview; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.ExpandableListView; | |
import android.widget.ExpandableListView.OnGroupClickListener; | |
import android.widget.ExpandableListView.OnGroupExpandListener; | |
import android.widget.Toast; | |
import com.demo.farooq.sublistlistview.ExpandableListAdapter; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import static android.widget.ExpandableListView.*; | |
/** | |
* Created by farooq on 1/4/2017. | |
*/ | |
public class MainActivity extends AppCompatActivity { | |
private static ExpandableListView expandableListView; | |
private static ExpandableListAdapter adapter; | |
HashMap<String, List<String>> hashMap; | |
ArrayList<String> header; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
expandableListView = (ExpandableListView) findViewById(R.id.simple_expandable_listview); | |
// Setting group indicator null for custom indicator | |
expandableListView.setGroupIndicator(null); | |
setItems(); | |
adapter = new ExpandableListAdapter(MainActivity.this, header, hashMap); | |
// Setting adpater for expandablelistview | |
expandableListView.setAdapter(adapter); | |
/* | |
You can add listeners for the item clicks | |
*/ | |
expandableListView.setOnGroupClickListener(new OnGroupClickListener() { | |
@Override | |
public boolean onGroupClick(ExpandableListView parent, View v, | |
int groupPosition, long id) { | |
return false; | |
} | |
}); | |
// Listview Group expanded listener | |
expandableListView.setOnGroupExpandListener(new OnGroupExpandListener() { | |
@Override | |
public void onGroupExpand(int groupPosition) { | |
Toast.makeText(getApplicationContext(), | |
header.get(groupPosition) + " Expanded", | |
Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
// Listview Group collasped listener | |
expandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() { | |
@Override | |
public void onGroupCollapse(int groupPosition) { | |
Toast.makeText(getApplicationContext(), | |
header.get(groupPosition) + " Collapsed", | |
Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
// Listview on child click listener | |
expandableListView.setOnChildClickListener(new OnChildClickListener() { | |
@Override | |
public boolean onChildClick(ExpandableListView parent, View v, | |
int groupPosition, int childPosition, long id) { | |
Toast.makeText( | |
getApplicationContext(), | |
header.get(groupPosition) | |
+ " : " | |
+ hashMap.get( | |
header.get(groupPosition)).get( | |
childPosition), Toast.LENGTH_SHORT) | |
.show(); | |
return false; | |
} | |
}); | |
} | |
// Setting headers and childs to expandable listview | |
void setItems() { | |
// Array list for header | |
header = new ArrayList<String>(); | |
// Array list for child items | |
List<String> child1 = new ArrayList<String>(); | |
List<String> child2 = new ArrayList<String>(); | |
List<String> child3 = new ArrayList<String>(); | |
List<String> child4 = new ArrayList<String>(); | |
// Hash map for both header and child | |
hashMap = new HashMap<String, List<String>>(); | |
// Adding headers to list | |
for (int i = 1; i < 5; i++) { | |
header.add("Group " + i); | |
} | |
// Adding child data | |
for (int i = 1; i < 5; i++) { | |
child1.add("Child" + i); | |
} | |
// Adding child data | |
for (int i = 1; i < 5; i++) { | |
child2.add("Child" + i); | |
} | |
// Adding child data | |
for (int i = 1; i < 6; i++) { | |
child3.add("Child" + i); | |
} | |
// Adding child data | |
for (int i = 1; i < 7; i++) { | |
child4.add("Child" + i); | |
} | |
// Adding header and childs to hash map | |
hashMap.put(header.get(0), child1); | |
hashMap.put(header.get(1), child2); | |
hashMap.put(header.get(2), child3); | |
hashMap.put(header.get(3), child4); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment