How do I implement ExpandableList in ViewPager in Android?
How do I implement ExpandableList in ViewPager in Android?
This is a simple 1 ExpandableList file 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 's where I learned to use the viewpager
now i want to use them together, but i don't know how :(
I tried to put the code in InstantiateIitem in the viewpager but it didn't work, I tried to make the viewpager an activity extending ExpandableListActivity, but that gave me a bunch of runtime exceptions that tried to fix one of them time, but what I achieved now is in the end kept trying things that didn't make sense to me anymore, so I stopped and asked here.
+1
source to share
1 answer
First of all add make a normal viewer and in the overridden instantiateItem method
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'm sure you are lost, so I made this example and posted it on github
+8
source to share