java - How can I put a ExpandableListView into a ScrollView without it collapsing? -
i want use expandablelistview inside scrollview other views faced problem of self scroller in expandablelistview l tried disable problem height of expandablelistview , layout it's inside. want
- disable expandablelistview scroll
- resize expandablelistview & linearlayout contains when groupview clicked
i googled solution , found 1 works listview only
i want make same workout expandablelistview (with custom adapter).
here code: mainactivity.java
package fablabegypt.android.expandablelistview2; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.motionevent; import android.view.view; import android.view.viewgroup; import android.widget.expandablelistview; import android.widget.linearlayout; import java.util.arraylist; import java.util.hashmap; import java.util.list; public class mainactivity extends actionbaractivity { mlistadapter listadapter; expandablelistview explistview; linearlayout linearlayout; list<string> listdataheader; hashmap<string, list<string>> listdatachild; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); linearlayout = (linearlayout) findviewbyid(r.id.linear_holder); preparelistdata(); explistview = (expandablelistview) findviewbyid(r.id.expand_list); //explistview.setscrollcontainer(false); explistview.sethorizontalscrollbarenabled(false); explistview.setverticalscrollbarenabled(false); explistview.setfastscrollenabled(false); explistview.setsmoothscrollbarenabled(false); //explistview.setoverscrollheader(null); explistview.setfooterdividersenabled(false); //explistview.setoverscrollfooter(null); //explistview.setverticalfadingedgeenabled(false); //explistview.sethorizontalfadingedgeenabled(false); explistview.setonchildclicklistener(new expandablelistview.onchildclicklistener() { @override public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) { return true; } }); explistview.setongroupclicklistener(new expandablelistview.ongroupclicklistener() { @override public boolean ongroupclick(expandablelistview parent, view v, int groupposition, long id) { if (parent.isgroupexpanded(groupposition)) { parent.collapsegroup(groupposition); } else { parent.expandgroup(groupposition); } //telling listview have handled group click, , don't want default actions. return true; } }); explistview.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (event.getaction() == motionevent.action_up) { return false; }else { } return true; } }); listadapter = new mlistadapter(this, listdataheader,listdatachild); 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); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { finish(); return true; } return super.onoptionsitemselected(item); } }
mlistadapter.java
package fablabegypt.android.expandablelistview2; import android.content.context; import android.database.datasetobserver; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.expandablelistadapter; import android.widget.textview; import java.util.hashmap; import java.util.list; /** * created mouso on 4/27/2015. */ public class mlistadapter extends baseexpandablelistadapter { private context context; private list<string> header_list; private hashmap<string, list<string>> children_data_list; public mlistadapter(context context,list<string> header_list,hashmap<string,list<string>> children_data_list){ this.context = context; this.header_list = header_list; this.children_data_list = children_data_list; } @override public void registerdatasetobserver(datasetobserver observer) { } @override public void unregisterdatasetobserver(datasetobserver observer) { } @override public int getgroupcount() { return this.children_data_list.size(); } @override public int getchildrencount(int groupposition) { return this.children_data_list.get(this.header_list.get(groupposition)).size(); } @override public object getgroup(int groupposition) { return this.header_list.get(groupposition); } @override public object getchild(int groupposition, int childposition) { return this.children_data_list.get(this.header_list.get(groupposition)).get(childposition); } @override public long getgroupid(int groupposition) { return groupposition; } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public boolean hasstableids() { return false; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { final string headertext = (string) getgroup(groupposition); if (convertview == null){ layoutinflater layoutinflater = (layoutinflater) this.context.getsystemservice(context.layout_inflater_service); convertview = layoutinflater.inflate(r.layout.list_group,null); } textview headertxt = (textview) convertview.findviewbyid(r.id.list_group_txt); headertxt.settextsize(20); headertxt.settext(headertext); //log.d("mouso",headertext); return convertview; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { final string childtext = (string) getchild(groupposition,childposition); if (convertview == null){ layoutinflater layoutinflater = (layoutinflater) this.context.getsystemservice(context.layout_inflater_service); convertview = layoutinflater.inflate(r.layout.list_item,null); } textview childtxt = (textview) convertview.findviewbyid(r.id.list_child_txt); childtxt.settext(childtext); return convertview; } @override public boolean ischildselectable(int groupposition, int childposition) { return false; } @override public boolean areallitemsenabled() { return true; } @override public boolean isempty() { if (header_list.size() > 0) return true; return false; } @override public void ongroupexpanded(int groupposition) { } @override public void ongroupcollapsed(int groupposition) { } @override public long getcombinedchildid(long groupid, long childid) { return (groupid*100)+childid; } @override public long getcombinedgroupid(long groupid) { return groupid; } }
activity_main.xml
<relativelayout 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:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <scrollview android:layout_width="wrap_content" android:layout_height="500px" android:fillviewport="true"> <linearlayout android:id="@+id/linear_holder" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:id="@+id/txt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="we 7yat 3neak fadaha 3neya \n\n\n dana ba7ebak ad 3naya"/> <expandablelistview android:id="@+id/expand_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:isscrollcontainer="false"> </expandablelistview> </linearlayout> </scrollview> </relativelayout>
list_group.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:id="@+id/list_group_txt" android:paddingleft="?android:attr/expandablelistpreferreditempaddingleft" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </linearlayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:id="@+id/list_child_txt" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </linearlayout>
my try implmenting workout
helper.java
package fablabegypt.android.expandablelistview2; import android.util.log; import android.view.view; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.expandablelistadapter; import android.widget.expandablelistview; import android.widget.listadapter; import android.widget.listview; /** * created mouso on 4/29/2015. */ public class listviewhelper { public static void getlistviewsize(expandablelistview mylistview) { listadapter mylistadapter = mylistview.getadapter(); if (mylistadapter == null) { //do nothing return null return; } //set listadapter in loop getting final size int totalheight = 0; (int groupsize = 0; groupsize < mylistadapter.getgroupcount(); groupsize++) { view listitem = mylistadapter.getgroupview(groupsize, false, null, mylistview); listitem.measure(0, 0); totalheight += listitem.getmeasuredheight(); (int size = 0; size < mylistadapter.getchildrencount(groupsize); size++) { if (size == mylistadapter.getchildrencount(groupsize)-1) listitem = mylistadapter.getchildview(groupsize, size, true, null, mylistview); listitem = mylistadapter.getchildview(groupsize, size, false, null, mylistview); listitem.measure(0, 0); totalheight += listitem.getmeasuredheight(); } } //setting listview item in adapter viewgroup.layoutparams params = mylistview.getlayoutparams(); params.height = totalheight + (mylistview.getdividerheight() * (mylistadapter.getgroupcount() - 1)); mylistview.setlayoutparams(params); // print height of adapter on log log.d("mouso", "height of listitem:"+string.valueof(totalheight)); } //read more: http://www.androidhub4you.com/2012/12/listview-into-scrollview-in-android.html#ixzz3yh4m4mpg }
it bad idea put view has it's own scrolling functionality inside view scrolling functionality. can lead touch actions being intercepted wrong view, poor performance, etc. also, main layout, don't seem using scrollview other making textview scroll list view. why not add header view? explistview.setheaderview(view)
, inflate textview it.
Comments
Post a Comment