Created
April 2, 2018 15:17
-
-
Save alitamoor65/9df922283d7aef0c6b637cc53bc7e0cd to your computer and use it in GitHub Desktop.
Expandable ListView
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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:orientation="vertical" | |
android:layout_height="match_parent" | |
tools:context="com.tutorialscafe.readallsms.MainActivity"> | |
<ExpandableListView | |
android:childDivider="#0000" | |
android:layout_margin="10dp" | |
android:id="@+id/lvExp" | |
android:layout_height="match_parent" | |
android:layout_width="match_parent"/> | |
</LinearLayout> |
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.tutorialscafe.readallsms; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.ExpandableListView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
/** | |
* ReadAllSMS | |
* Created by Ali Tamoor on 6/1/2017. | |
*/ | |
public class MainActivity extends AppCompatActivity{ | |
ExpandableListAdapter listAdapter; | |
ExpandableListView expListView; | |
List<String> listDataHeader; | |
HashMap<String, List<String>> listDataChild; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
expListView = (ExpandableListView) findViewById(R.id.lvExp); | |
prepareListData(); | |
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); | |
expListView.setAdapter(listAdapter); | |
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { | |
@Override | |
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { | |
Toast.makeText(MainActivity.this, " " + listAdapter.getChild(groupPosition,childPosition), Toast.LENGTH_SHORT).show(); | |
return true; | |
} | |
}); | |
} | |
private void prepareListData() { | |
listDataHeader = new ArrayList<String>(); | |
listDataChild = new HashMap<String, List<String>>(); | |
// Adding child data | |
listDataHeader.add("1-Introduction"); | |
listDataHeader.add("2-Bsics of C++"); | |
listDataHeader.add("3-Program Structure"); | |
listDataHeader.add("4-Compund Data Types"); | |
listDataHeader.add("5-Classes"); | |
// Adding child data | |
List<String> introduction = new ArrayList<String>(); | |
introduction.add("Compiler"); | |
List<String> basic = new ArrayList<String>(); | |
basic.add("Constrol Structure"); | |
basic.add("Functions"); | |
basic.add("Overloads and Teplates"); | |
basic.add("Name Visibilities"); | |
List<String> structure = new ArrayList<String>(); | |
structure.add("Arrays"); | |
structure.add("Charater Sequesnce"); | |
structure.add("Pointers"); | |
structure.add("Dynamic Memory"); | |
structure.add("Data Structure"); | |
structure.add("Other Data Types"); | |
List<String> datatupes = new ArrayList<String>(); | |
datatupes.add("Arrays"); | |
datatupes.add("Charater Sequesnce"); | |
datatupes.add("Pointers"); | |
datatupes.add("Dynamic Memory"); | |
datatupes.add("Data Structure"); | |
datatupes.add("Other Data Types"); | |
List<String> classes = new ArrayList<String>(); | |
classes.add("CLasses (I)"); | |
classes.add("Classes (II)"); | |
classes.add("Special Members"); | |
classes.add("Friendship and inhereitence"); | |
classes.add("Polymorphism"); | |
listDataChild.put(listDataHeader.get(0), introduction); // Header, Child data | |
listDataChild.put(listDataHeader.get(1), basic); | |
listDataChild.put(listDataHeader.get(2), structure); | |
listDataChild.put(listDataHeader.get(3), datatupes); | |
listDataChild.put(listDataHeader.get(4), classes); | |
} | |
} |
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.tutorialscafe.readallsms; | |
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseExpandableListAdapter; | |
import android.widget.TextView; | |
import java.util.HashMap; | |
import java.util.List; | |
/** | |
* ReadAllSMS | |
* Created by Ali Tamoor on 6/3/2017. | |
*/ | |
public class ExpandableListAdapter extends BaseExpandableListAdapter { | |
private Context _context; | |
private List<String> _listDataHeader; // header titles | |
// child data in format of header title, child title | |
private HashMap<String, List<String>> _listDataChild; | |
public ExpandableListAdapter(Context context, List<String> listDataHeader, | |
HashMap<String, List<String>> listChildData) { | |
this._context = context; | |
this._listDataHeader = listDataHeader; | |
this._listDataChild = listChildData; | |
} | |
@Override | |
public Object getChild(int groupPosition, int childPosititon) { | |
return this._listDataChild.get(this._listDataHeader.get(groupPosition)) | |
.get(childPosititon); | |
} | |
@Override | |
public long getChildId(int groupPosition, int childPosition) { | |
return childPosition; | |
} | |
@Override | |
public View getChildView(int groupPosition, final int childPosition, | |
boolean isLastChild, View convertView, ViewGroup parent) { | |
final String childText = (String) getChild(groupPosition, childPosition); | |
if (convertView == null) { | |
LayoutInflater infalInflater = (LayoutInflater) this._context | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
convertView = infalInflater.inflate(R.layout.list_item, null); | |
} | |
TextView txtListChild = (TextView) convertView | |
.findViewById(R.id.lblListItem); | |
txtListChild.setText(childText); | |
return convertView; | |
} | |
@Override | |
public int getChildrenCount(int groupPosition) { | |
return this._listDataChild.get(this._listDataHeader.get(groupPosition)) | |
.size(); | |
} | |
@Override | |
public Object getGroup(int groupPosition) { | |
return this._listDataHeader.get(groupPosition); | |
} | |
@Override | |
public int getGroupCount() { | |
return this._listDataHeader.size(); | |
} | |
@Override | |
public long getGroupId(int groupPosition) { | |
return groupPosition; | |
} | |
@Override | |
public View getGroupView(int groupPosition, boolean isExpanded, | |
View convertView, ViewGroup parent) { | |
String headerTitle = (String) getGroup(groupPosition); | |
if (convertView == null) { | |
LayoutInflater infalInflater = (LayoutInflater) this._context | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
convertView = infalInflater.inflate(R.layout.list_group, null); | |
} | |
TextView lblListHeader = (TextView) convertView | |
.findViewById(R.id.lblListHeader); | |
lblListHeader.setTypeface(null, Typeface.BOLD); | |
lblListHeader.setText(headerTitle); | |
return convertView; | |
} | |
@Override | |
public boolean hasStableIds() { | |
return false; | |
} | |
@Override | |
public boolean isChildSelectable(int groupPosition, int childPosition) { | |
return true; | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:padding="2dp" | |
android:background="#58D68D"> | |
<TextView | |
android:paddingLeft="40dp" | |
android:height="50dp" | |
android:gravity="center_vertical" | |
android:text="Here your Group Goes" | |
android:id="@+id/lblListHeader" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:textSize="17dp" | |
android:textColor="#FFFF" /> | |
</LinearLayout> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="50dip" | |
android:orientation="vertical" > | |
<TextView | |
android:background="#F0F3F4" | |
android:layout_marginLeft="5dp" | |
android:layout_marginRight="5dp" | |
android:gravity="center_vertical" | |
android:text="@string/app_name" | |
android:height="50dp" | |
android:id="@+id/lblListItem" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:textSize="17dip" | |
android:paddingTop="5dp" | |
android:paddingBottom="5dp" | |
android:paddingLeft="50dp" | |
/> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment