Expendablelistview means load different resources by clicking on the children of the expandable list
Here we can see the expendable listview below Example
For Example arrow image icon that is used to collapse/expand the list
we need to create adapter which extends BaseExpandableListAdapter it will manage child view and parent view . This we can see the example below
ExpendableListActivity[ExpendableListActivity.java]
package com.androidfeeders.expendablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.Toast;
public class ExpendableListActivity extends AppCompatActivity {
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List expandableListHeader;
HashMap> expandableListDetail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListDetail = ExpandableList.getData();
expandableListHeader = new ArrayList(expandableListDetail.keySet());
expandableListAdapter = new ExpandableListAdapter(this, expandableListHeader, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
parent.smoothScrollToPosition(groupPosition);
if (parent.isGroupExpanded(groupPosition)) {
ImageView imageView = v.findViewById(R.id.expandable_icon);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_keyboard_arrow_down_black_24dp));
} else {
ImageView imageView = v.findViewById(R.id.expandable_icon);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_keyboard_arrow_up_black_24dp));
}
showToastMessage(expandableListHeader.get(groupPosition));
return false ;
}
});
expandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
showToastMessage(expandableListHeader.get(groupPosition));
}
});
expandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
showToastMessage(expandableListDetail.get(expandableListHeader.get(groupPosition)).get(childPosition));
return false;
}
});
}
public void showToastMessage(String message) {
Toast.makeText(ExpendableListActivity.this, message, Toast.LENGTH_SHORT).show();
}
}
ExpendableList[ExpendableList.java]
package com.androidfeeders.expendablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ExpandableList {
public static HashMap> getData() {
HashMap> expandableListDetail = new HashMap>();
List quarterBack = new ArrayList();
quarterBack.add(" Kevin Burke");
quarterBack.add("Dylan Favre");
List offensiveTeam = new ArrayList();
offensiveTeam.add("James Atoe");
offensiveTeam.add("Mike Criste");
offensiveTeam.add("Jeremy Galten");
offensiveTeam.add("Randall Harris");
offensiveTeam.add("Alex Land");
List defensive = new ArrayList();
defensive.add("Calvin Burnett Jr.");
defensive.add("Bryan Douglas");
defensive.add("Lucky Dozier");
defensive.add("Curtis Slater");
defensive.add("Cliff Stokes");
List linebackers = new ArrayList();
linebackers.add("B. J. Beatty");
linebackers.add("David Guthrie");
linebackers.add("Matt Oh");
linebackers.add("Scott Thompson");
linebackers.add("Talib Wise");
expandableListDetail.put("QUATERBACK TEAM", quarterBack);
expandableListDetail.put("OFFENSIVE TEAM", offensiveTeam);
expandableListDetail.put("DEFENSIVE", defensive);
expandableListDetail.put("LINEBACKERS", linebackers);
return expandableListDetail;
}
}
ExpandableListAdapter[ExpandableListAdapter.java]
package com.androidfeeders.expendablelistview;
import java.util.HashMap;
import java.util.List;
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;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List expandableListHeader;
private HashMap> expandableListDetail;
public ExpandableListAdapter(Context context, List expandableListHeader,
HashMap> expandableListDetail) {
this.context = context;
this.expandableListHeader = expandableListHeader;
this.expandableListDetail = expandableListDetail;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListHeader.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListHeader.get(listPosition))
.size();
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListHeader.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListHeader.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
Layout Resource[expend_list_activity.xml]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ExpendableListActivity">
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:divider="@color/colorAccent"
android:groupIndicator="@null"
android:dividerHeight="1dp" />
</RelativeLayout>
Layout Resource[list_group.xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textColor="#A4C739"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<ImageView
android:id="@+id/expandable_icon"
android:layout_alignParentEnd="true"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="6dp"
android:src="@drawable/ic_keyboard_arrow_down_black_24dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
Layout Resource[list_item.xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/expandedListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</LinearLayout>
Gradle Scripts~build.gradle[Module:app]
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.androidfeeders.expendablelistview"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Gradle Scripts~build.gradle[Project:ExpendableListView]
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Resource[strings.xml]
<resources>
<string name="app_name">ExpendAbleListView</string>
</resources>
manifests
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidfeeders.expendablelistview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ExpendableListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I started to learn android and Kotlin and iOS and Hybrid application and i cant code IOS very well. But i love more interesting to code and finding new developer minds who are join to me You're welcome to keep on using my website.Instead, you can join our FB page for developer comments and post there: https://www.facebook.com/ramasamy.m.779 Best wishes :) Ramasamy(Software Developer)
See what's new in Android & IOS & Hybrid development … explore and learn in site
Ramasamy
5 min agoKotlin and React native example too
ReplyRamasamy
5 min agoKotlin and React native example too
ReplyRamasamy
5 min agoKotlin and React native example too
Reply