ExpandableListView with Listeners |
package com.example.androidexpandablelistview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupClickListener; import android.widget.ExpandableListView.OnGroupCollapseListener; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.TextView; public class MainActivity extends Activity { MyBaseExpandableListAdapter myBaseExpandableListAdapter; ExpandableListView myExpandableListView; List<String> myListForGroup; HashMap<String, List<String>> myMapForChild; TextView info; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); info = (TextView)findViewById(R.id.info); myExpandableListView = (ExpandableListView) findViewById(R.id.myexpandablelistview); initData(); myBaseExpandableListAdapter = new MyBaseExpandableListAdapter(this, myListForGroup, myMapForChild); myExpandableListView.setAdapter(myBaseExpandableListAdapter); myExpandableListView.setOnChildClickListener(myOnChildClickListener); myExpandableListView.setOnGroupClickListener(myOnGroupClickListener); myExpandableListView.setOnGroupCollapseListener(myOnGroupCollapseListener); myExpandableListView.setOnGroupExpandListener(myOnGroupExpandListener); } private void initData() { myListForGroup = new ArrayList<String>(); myMapForChild = new HashMap<String, List<String>>(); List<String> listGroupA = new ArrayList<String>(); listGroupA.add("A - 1"); listGroupA.add("A - 2"); listGroupA.add("A - 3"); List<String> listGroupB = new ArrayList<String>(); listGroupB.add("B - 1"); List<String> listGroupC = new ArrayList<String>(); listGroupC.add("C - 1"); listGroupC.add("C - 2"); myListForGroup.add("Group A"); myListForGroup.add("Group B"); myListForGroup.add("Group C"); myMapForChild.put(myListForGroup.get(0), listGroupA); myMapForChild.put(myListForGroup.get(1), listGroupB); myMapForChild.put(myListForGroup.get(2), listGroupC); } OnChildClickListener myOnChildClickListener = new OnChildClickListener(){ @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { String strChild = (String)myBaseExpandableListAdapter .getChild(groupPosition, childPosition); info.setText("child clicked: " + strChild); return true; }}; OnGroupClickListener myOnGroupClickListener = new OnGroupClickListener(){ @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { String strGroup = (String)myBaseExpandableListAdapter .getGroup(groupPosition); info.setText("group clicked: " + strGroup); return false; //return false //otherwise, group will not expand when user click on it }}; OnGroupCollapseListener myOnGroupCollapseListener = new OnGroupCollapseListener(){ @Override public void onGroupCollapse(int groupPosition) { info.setText("group collapse: " + groupPosition); }}; OnGroupExpandListener myOnGroupExpandListener = new OnGroupExpandListener(){ @Override public void onGroupExpand(int groupPosition) { info.setText("group expand: " + groupPosition); }}; }
/res/layout/activity_main.xml, modify to add a TextView to display info.
<LinearLayout 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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" android:textStyle="bold" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ExpandableListView android:id="@+id/myexpandablelistview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
For other files, MyBaseExpandableListAdapter.java, group_layout.xml and item_layout.xml, refer to ExpandableListView example.
Next:
- ExpandableListView with icon
No comments:
Post a Comment