android- Expandable List not working inside viewpager fragment -
i want have expandable list inside viewpager . viewpager child fragment . i've used tutorial : http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
this fragment code :
import java.util.arraylist; import java.util.hashmap; import java.util.list; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.expandablelistview; public class resturant_menu extends fragment { expandablelistadapter listadapter; expandablelistview explistview; list<string> listdataheader; hashmap<string, list<string>> listdatachild; view v; @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { v = inflater.inflate(r.layout.resturantmenu, container, false); return v; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); // listview explistview = (expandablelistview) v.findviewbyid(r.id.expandablelistview1); // preparing list data preparelistdata(); listadapter = new expandablelistadapter(getactivity(), listdataheader, listdatachild); // setting list adapter explistview.setadapter(listadapter); } private void preparelistdata() { listdataheader = new arraylist<string>(); listdatachild = new hashmap<string, list<string>>(); // adding child data listdataheader.add("top 250"); listdataheader.add("now showing"); listdataheader.add("coming soon.."); // adding child data list<string> top250 = new arraylist<string>(); top250.add("the shawshank redemption"); top250.add("the godfather"); top250.add("the godfather: part ii"); top250.add("pulp fiction"); top250.add("the good, bad , ugly"); top250.add("the dark knight"); top250.add("12 angry men"); list<string> nowshowing = new arraylist<string>(); nowshowing.add("the conjuring"); nowshowing.add("despicable me 2"); nowshowing.add("turbo"); nowshowing.add("grown ups 2"); nowshowing.add("red 2"); nowshowing.add("the wolverine"); list<string> comingsoon = new arraylist<string>(); comingsoon.add("2 guns"); comingsoon.add("the smurfs 2"); comingsoon.add("the spectacular now"); comingsoon.add("the canyons"); comingsoon.add("europa report"); listdatachild.put(listdataheader.get(0), top250); // header, child data listdatachild.put(listdataheader.get(1), nowshowing); listdatachild.put(listdataheader.get(2), comingsoon); }
}
this expandablelistadapter:
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<string> _listdataheader; // header titles // child data in format of header title, child title private hashmap<string, list<string>> _listdatachild; public expandablelistadapter(context context, list<string> listdataheader, hashmap<string, list<string>> listchilddata) { this._context = context; this._listdataheader = listdataheader; this._listdatachild = listchilddata; } @override public object getchild(int groupposition, int childposititon) { return this._listdatachild.get(this._listdataheader.get(groupposition)) .get(childposititon); } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public view getchildview(int groupposition, final int childposition, boolean islastchild, view convertview, viewgroup parent) { final string childtext = (string) getchild(groupposition, childposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.list_item, null); } textview txtlistchild = (textview) convertview .findviewbyid(r.id.lbllistitem); txtlistchild.settext(childtext); return convertview; } @override public int getchildrencount(int groupposition) { return this._listdatachild.get(this._listdataheader.get(groupposition)) .size(); } @override public object getgroup(int groupposition) { return this._listdataheader.get(groupposition); } @override public int getgroupcount() { return this._listdataheader.size(); } @override public long getgroupid(int groupposition) { return groupposition; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { string headertitle = (string) getgroup(groupposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.list_group, null); } textview lbllistheader = (textview) convertview .findviewbyid(r.id.lbllistheader); lbllistheader.settypeface(null, typeface.bold); lbllistheader.settext(headertitle); return convertview; } @override public boolean hasstableids() { return false; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; }
}
this activity :
public class showit extends fragmentactivity { private pagerslidingtabstrip tabs; private viewpager pager; private mypageradapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.showit); tabs = (pagerslidingtabstrip) findviewbyid(r.id.tabs); pager = (viewpager) findviewbyid(r.id.pager); adapter = new mypageradapter(getsupportfragmentmanager()); pager.setadapter(adapter); final int pagemargin = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, 4, getresources().getdisplaymetrics()); pager.setpagemargin(pagemargin); tabs.setviewpager(pager); } public class mypageradapter extends fragmentpageradapter { private final string[] titles = getresources().getstringarray(r.array.tabresturan);; public mypageradapter(fragmentmanager fm) { super(fm); } @override public charsequence getpagetitle(int position) { return titles[position]; } @override public int getcount() { return titles.length; } @override public fragment getitem(int arg0) { fragment fragment=null; fragment=new resturant_menu(); return fragment; } }
this layout :
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <expandablelistview android:id="@+id/expandablelistview1" android:layout_width="match_parent" android:layout_height="wrap_content" > </expandablelistview>
this :
instead of :
i no error , i've added classes , layout that's been said .
could me solve ? you
try setting height of expandablelistview match parent. think should trick
Comments
Post a Comment