Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is something that will help you, I have done something like that in my application.</p> <p>First create some xml to hold your data This is my activity_main</p> <pre><code> &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" 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" &gt; &lt;ExpandableListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/listbg" &gt; &lt;/ExpandableListView&gt; </code></pre> <p> </p> <p>Xml 2; listrow_details</p> <pre><code> &lt;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="40dp" android:clickable="true" android:orientation="vertical" android:paddingLeft="40dp" tools:context=".MainActivity" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:drawablePadding="5dp" android:gravity="center_vertical" android:text="@string/hello_world" android:textSize="14sp" android:textStyle="bold" &gt; &lt;/TextView&gt; &lt;View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /&gt; </code></pre> <p> </p> <p>Xml 3 : listview_group</p> <pre><code> &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="?android:attr/listPreferredItemHeight" android:layout_marginLeft="8dp" android:gravity="left" android:paddingLeft="32dp" android:paddingTop="8dp" android:text="Test" android:textSize="14sp" android:textAlignment="textEnd" android:textStyle="bold" /&gt; </code></pre> <p>Create a group class:</p> <pre><code> public class Group { public String string; public final List&lt;String&gt; children = new ArrayList&lt;String&gt;(); public Group(String string) { this.string = string; } } </code></pre> <p>A adapter class for your expandable list view:</p> <pre><code> public class MyExpandableListAdapter extends BaseExpandableListAdapter { private final SparseArray&lt;Group&gt; groups; public LayoutInflater inflater; public Activity activity; public MyExpandableListAdapter(Activity act, SparseArray&lt;Group&gt; groups) { activity = act; this.groups = groups; inflater = act.getLayoutInflater(); } @Override public Object getChild(int groupPosition, int childPosition) { return groups.get(groupPosition).children.get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String children = (String) getChild(groupPosition, childPosition); TextView text = null; if (convertView == null) { convertView = inflater.inflate(R.layout.listrow_details, null); } text = (TextView) convertView.findViewById(R.id.textView1); text.setText(children); convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(activity, children, Toast.LENGTH_SHORT).show(); } }); return convertView; } @Override public int getChildrenCount(int groupPosition) { return groups.get(groupPosition).children.size(); } @Override public Object getGroup(int groupPosition) { return groups.get(groupPosition); } @Override public int getGroupCount() { return groups.size(); } @Override public void onGroupCollapsed(int groupPosition) { super.onGroupCollapsed(groupPosition); } @Override public void onGroupExpanded(int groupPosition) { super.onGroupExpanded(groupPosition); } @Override public long getGroupId(int groupPosition) { return 0; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ExpandableListView eLV = (ExpandableListView) parent; eLV.expandGroup(groupPosition); if (convertView == null) { convertView = inflater.inflate(R.layout.listview_group, null); } Group group = (Group) getGroup(groupPosition); ((TextView) convertView).setText(group.string); // ((CheckedTextView) convertView).setChecked(isExpanded); return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } } </code></pre> <p>and finally do this in your main activity</p> <pre><code> public class MainActivity extends Activity { // More efficient than HashMap for mapping integers to objects SparseArray&lt;Group&gt; groups = new SparseArray&lt;Group&gt;(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createData(); ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView); listView.setGroupIndicator(getResources().getDrawable(R.drawable.listbg)); MyExpandableListAdapter adapter = new MyExpandableListAdapter(this, groups); listView.setAdapter(adapter); } public void createData() { for (int j = 0; j &lt; 5; j++) { Group group = new Group("Test " + j); for (int i = 0; i &lt; 5; i++) { group.children.add("Sub Item" + i); } groups.append(j, group); } } } </code></pre> <p>Above code will solve your problem, I have used a drawable xml to customize look for my expendablelistview</p> <p>here it is listbg.xml</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_empty="true" android:drawable="@android:color/transparent"/&gt; &lt;item android:state_expanded="true" android:drawable="@android:color/transparent" /&gt; &lt;item android:drawable="@android:color/transparent" /&gt; &lt;/selector&gt; </code></pre> <p>You can use buttons or anything to customize according to your needs. This code will solve your problem i suppose, PEACE:)</p> <p><strong>Screenshot</strong></p> <p><img src="https://i.stack.imgur.com/jzHen.jpg" alt="This is how it will look like"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload