in this we will discuss of creation of listview with expandable option, using simple xml and java.
step 1) create activity-main.xml file and add relative layout,
within this relative layout use below code,
<ExpandableListView
android:id="@+id/expandList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/background_light"
android:dividerHeight="0.5dp"/>
step 2) go to Mainactivity.java in oncreate method put below code,
expandableListView = findViewById(R.id.expendableList);
expandableListDetail = ExpandableListmyclass.getData();
expandableListTitle = new ArrayList<>(expandableListDetail.keySet());
expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(), expandableListTitle.get(groupPosition)
+ " List Expanded.", Toast.LENGTH_SHORT).show();
}
and simply create setOnGroupCollapseListener and setOnChildClickListener,
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(), expandableListTitle.get(groupPosition) + " List Collapsed.",
Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText( getApplicationContext(), expandableListTitle.get(groupPosition) + "
-> " + expandableListDetail.get( expandableListTitle.get(groupPosition)).get( childPosition), Toast.LENGTH_SHORT ).show();
return false;
}
});
create new class ExpandableListmyclass by creating new file ExpandableListmyclass.java
simply add below code in class ,
static HashMap<String, List<String>> getData() {
HashMap<String, List<String>> expandableListDetail = new HashMap<>();
List<String> myFavCricketPlayers = new ArrayList<>();
myFavCricketPlayers.add("MS.Dhoni");
myFavCricketPlayers.add("Sehwag");
myFavCricketPlayers.add("Watson");
myFavCricketPlayers.add("sachin tendulkar");
myFavCricketPlayers.add("rahul dravid");
expandableListDetail.put("CRICKET PLAYERS", myFavCricketPlayers);
}
then add row list
<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/listTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@android:color/black" />
</LinearLayout>
and manifest file no change.
it's time to go and check on emulator or real device. please comment your message "success", if it works perfect, otherwise comment me if an error occurs.
No comments:
Post a Comment