Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6811009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:16:29+00:00 2026-05-26T20:16:29+00:00

How to implement an ExpandableList in a ViewPager in Android ? This is a

  • 0

How to implement an ExpandableList in a ViewPager in Android ?

This is a simple 1 file ExpandableList provided by google

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Demonstrates expandable lists backed by a Simple Map-based adapter
 */
public class ExpandableList3 extends ExpandableListActivity {
    private static final String NAME = "NAME";
    private static final String IS_EVEN = "IS_EVEN";

    private ExpandableListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
        for (int i = 0; i < 20; i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, "Group " + i);
            curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (int j = 0; j < 15; j++) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
                curChildMap.put(NAME, "Child " + j);
                curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
            }
            childData.add(children);
        }

        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(
                this,
                groupData,
                android.R.layout.simple_expandable_list_item_1,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 },
                childData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 }
                );
        setListAdapter(mAdapter);
    }

}

And here is where i learned to use the viewpager

now i want to use those together, but i don’t know how 🙁

I’ve tried to put the code in InstantiateIitem within the viewpager, but that didn’t work, i tried making the viewpager an activty that extenda ExpandableListActivity, but it gave me a bunch of runtime exceptions kept trying o fix one at a time, but reached nowwhere at the end, i kept trying stuff that didn’t make sense to me anymore so i stopped and asked here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:16:30+00:00Added an answer on May 26, 2026 at 8:16 pm

    First of all add make a normal view pager and in the overridden method instantiateItem

                    if(position == 2) {// third page
                            View v = inflater.inflate(R.layout.expander, null, false);
                            ExpandableListView elv1 = (ExpandableListView) v.findViewById(R.id.elv1);
                            mAdapter = new BaseExpandableListAdapter() {
    
                        @Override
                        public Object getChild(int groupPosition,
                                int childPosition) {
                            return null;
                        }
    
                        @Override
                        public long getChildId(int groupPosition,
                                int childPosition) {
                            return 0;
                        }
    
                        @Override
                        public int getChildrenCount(int groupPosition) {
                            return 3; // Size of children usually taken from
                                        // .length or .size()
                        }
    
                        @Override
                        public View getChildView(int groupPosition,
                                int childPosition, boolean isLastChild,
                                View convertView, ViewGroup parent) {
                            View v = inflater.inflate(R.layout.twolinelistitem,
                                    null, false);
                            TextView tv = (TextView) v
                                    .findViewById(R.id.simple_expandable_list_item_2_text1);
                            tv.setText("Child " + (childPosition + 1)
                                    + " of group " + (groupPosition + 1));
                            return v;
                        }
    
                        @Override
                        public Object getGroup(int groupPosition) {
                            return null;
                        }
    
                        @Override
                        public int getGroupCount() {
                            return 12; // Groups count usually taken from
                                        // .length or .size()
                        }
    
                        @Override
                        public long getGroupId(int groupPosition) {
                            return 0;
                        }
    
                        @Override
                        public View getGroupView(int groupPosition,
                                boolean isExpanded, View convertView,
                                ViewGroup parent) {
                            View v = inflater.inflate(R.layout.twolinelistitem,
                                    parent, false);
                            TextView tv = (TextView) v
                                    .findViewById(R.id.simple_expandable_list_item_2_text1);
                            tv.setText("Group " + (groupPosition + 1));
                            return v;
                        }
    
                        @Override
                        public boolean hasStableIds() {
                            return false;
                        }
    
                        @Override
                        public boolean isChildSelectable(int groupPosition,
                                int childPosition) {
                            return true;
                        }
                    };
                    elv1.setAdapter(ViewPagerPlusExpandableListActivity.this.mAdapter);
    
                    ((ViewPager) collection).addView(v, 0);
                    return v;
                }
    

    I am sure you are lost, this is why i made this example and posted it on github

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement something like this: <div> <table> <thead> <tr> <td>Port name</td> <td>Current
I'm going to implement JSON-RPC web service. I need specifications for this. So far
I'm trying to implement a Cometd/Bayeux server on Android using iJetty. The Jetty implementation
I am trying to implement a simple digital clock with second and two colons
I was trying to implement a simple function that can concatenate any number of
I implement this interleave method in java but it doesn't work properly. Where is
Implement double sqrt(double x) in C++ without using std library. This is a facebook
I implement very big script logic using jquery plugin at caret(http://code.google.com/p/jquery-at-caret/), but now when
There are no controls in Android that provide Tree-like View. There is an ExpandableList
Golf - implement a simple templating scheme. Expansions are: %KEY% -> VALUE %% ->

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.