MainActivity.java
=======================
package com.sam.web.expandablelistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
private CustomAdapter customAdapter;
List<String> listDataHeader;
HashMap<String,List<String>>listDataChild;
private int lastExpandablePosition=-1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareListData();
expandableListView=(ExpandableListView) findViewById(R.id.expandableListViewId);
customAdapter=new CustomAdapter(this,listDataHeader,listDataChild);
expandableListView.setAdapter(customAdapter);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int i, long id) {
String groupName=listDataHeader.get(i);
Toast.makeText(getApplicationContext(),"group Name :"+groupName,Toast.LENGTH_SHORT).show();
return false;
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int i) {
String groupName=listDataHeader.get(i);
Toast.makeText(getApplicationContext(),groupName+"is Collapsed",Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int i, int il, long id) {
String childString=listDataChild.get(listDataHeader.get(i)).get(il);
Toast.makeText(getApplicationContext(),childString,Toast.LENGTH_SHORT).show();
return false;
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int i) {
if(lastExpandablePosition!=-1 && lastExpandablePosition!=i){
expandableListView.collapseGroup(lastExpandablePosition);
}
lastExpandablePosition=i;
}
});
}
private void prepareListData() {
String[] headerString=getResources().getStringArray(R.array.abbreviations_list_header);
String[] childString=getResources().getStringArray(R.array.abbreviations_list_child);
listDataHeader=new ArrayList<>();
listDataChild=new HashMap<>();
for (int i=0; i<headerString.length; i++){
// adding header data
listDataHeader.add(headerString[i]);
List<String> child=new ArrayList<>();
child.add(childString[i]);
listDataChild.put(listDataHeader.get(i),child);
}
}
}
thistimebd Bangladesh Live online newsportal, education, Lifestyle, Health, Photography, gif image etc.
Make your own name or company name website | contact: thistimebd24@gmail.com
Copyright © 2020-2024 News Portal in Bangladesh - THISTIMEBD.COM. ALL Rights Reserved.