java - Expandable listview with indicator image -
i have customized expandable listview. want arrow on left side of every group. tried (note section "getgroupview
"):
package com.aaron.waller.mrpolitik.tabs; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.expandablelistview; import android.widget.imageview; import android.widget.textview; import android.widget.toast; import com.aaron.waller.mrpolitik.r; /** * created oclemmy on 5/10/2016 programmingwizards channel , http://www.camposha.com. */ public class fragenfragment extends fragment { view rootview; expandablelistview lv; private string[] groups; private string[][] children; public imageview groupholder; public fragenfragment() { } public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); groups = new string[]{"ich habe einen eiswürfel verschluckt und ihn noch nicht ausgekackt, steckt er fest?", "warum macht meine katze komische geräusche wenn ich micht auf sie stelle?", "wird mein laptop schwerer, wenn ich mehr dateien draflade?", "warum vibriert meine katze?", "platzhalterfrage","" + "platzhalterfrage", "platzhalterfrage", "platzhalterfrage", "platzhalterfrage", "platzhalterfrage", "platzhalterfrage", "platzhalterfrage", "platzhalterfrage", "platzhalterfrage"}; children = new string[][]{ {"s dummy text of printing , typesetting industry. lorem ipsum has been industry's standard dummy text ever since 1500s, when unknown printer took galley of type , scrambled make type specimen book. has survived not 5 centuries, leap electronic typesetting, remaining unchanged. popularised in 1960s release of letraset sheets containing lorem ipsum passages, , more desktop publishing software aldus pagemaker including versions of lorem ipsum."}, {"contrary popular belief, lorem ipsum not random text. has roots in piece of classical latin literature 45 bc, making on 2000 years old. richard mcclintock, latin professor @ hampden-sydney college in virginia, looked 1 of more obscure latin words, consectetur, lorem ipsum passage, , going through cites of word in classical literature, discovered undoubtable source. lorem ipsum comes sections 1.10.32 , 1.10.33 of comes line in section 1.10.32."}, {"it long established fact reader distracted readable content of page when looking @ layout. point of using lorem ipsum has more-or-less normal distribution of letters, opposed using 'content here, content here', making readable english. many desktop publishing packages , web page editors use lorem ipsum default model text, , search 'lorem ipsum' uncover many web sites still in infancy. various versions have evolved on years, accident, on purpose (injected humour , like)."}, {"there many variations of passages of lorem ipsum available, majority have suffered alteration in form, injected humour, or randomised words don't believable. if going use passage of lorem ipsum, need sure there isn't embarrassing hidden in middle of text. lorem ipsum generators on internet tend repeat predefined chunks necessary, making first true generator on internet. uses dictionary of on 200 latin words, combined handful of model sentence structures, generate lorem ipsum looks reasonable. generated lorem ipsum therefore free repetition, injected humour, or non-characteristic words etc."}, {"platzhalterantwort"}, {"platzhalterantwort"}, {"platzhalterantwort"}, {"platzhalterantwort"}, {"platzhalterantwort"}, {"platzhalterantwort"}, {"platzhalterantwort"}, {"platzhalterantwort"}, {"platzhalterantwort"}, {"platzhalterantwort"}, }; } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragen_layout, container, false); return rootview; } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); lv = (expandablelistview) view.findviewbyid(r.id.explistview); lv.setadapter(new expandablelistadapter(groups, children)); lv.setgroupindicator(null); groupholder = (imageview) view.findviewbyid(r.id.groupholder); /* displaymetrics metrics = new displaymetrics(); ((mainactivity)getcontext()).getwindowmanager().getdefaultdisplay().getmetrics(metrics); int width = metrics.widthpixels; lv.setindicatorbounds(width - getpixelfromdips(50), width - getpixelfromdips(10)); */ } /* public int getpixelfromdips(float pixels) { // screen's density scale final float scale = getresources().getdisplaymetrics().density; // convert dps pixels, based on density scale return (int) (pixels * scale + 0.5f); } */ public class expandablelistadapter extends baseexpandablelistadapter { private final layoutinflater inf; private string[] groups; private string[][] children; public expandablelistadapter(string[] groups, string[][] children) { this.groups = groups; this.children = children; inf = layoutinflater.from(getactivity()); } @override public int getgroupcount() { return groups.length; } @override public int getchildrencount(int groupposition) { return children[groupposition].length; } @override public object getgroup(int groupposition) { return groups[groupposition]; } @override public object getchild(int groupposition, int childposition) { return children[groupposition][childposition]; } @override public long getgroupid(int groupposition) { return groupposition; } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public boolean hasstableids() { return true; } @override public view getchildview(int groupposition, final int childposition, boolean islastchild, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = inf.inflate(r.layout.list_item, parent, false); holder = new viewholder(); holder.text = (textview) convertview.findviewbyid(r.id.lbllistitem); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.text.settext(getchild(groupposition, childposition).tostring()); return convertview; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = inf.inflate(r.layout.list_group, parent, false); holder = new viewholder(); holder.text = (textview) convertview.findviewbyid(r.id.groupholder1); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } if (isexpanded) { groupholder.setimageresource(r.drawable.arrow1); } else { groupholder.setimageresource(r.drawable.arrow2); } } holder.text.settext(getgroup(groupposition).tostring()); return convertview; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } private class viewholder { textview text; } } }
but dont know put these lines:
if (isexpanded) { groupholder.setimageresource(r.drawable.arrow1); } else { groupholder.setimageresource(r.drawable.arrow2); }
at moment app crashes if try open fragment expandable listview.
this expandable listview:
<expandablelistview android:layout_margintop="45dp" android:id="@+id/explistview" android:layout_height="match_parent" android:groupindicator="@null" android:layout_width="match_parent"/>
and list_group.xml customized group design:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <relativelayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="vertical" > <imageview android:id="@+id/groupholder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow2" android:layout_centervertical="true" /> <textview android:id="@+id/groupholder1" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:paddingleft="6dip" android:paddingtop="6dip" android:textcolor="#282828" android:textalignment="center" android:textsize="20dp" tools:ignore="spusage" android:layout_marginbottom="10dp" android:layout_margintop="10dp"/> </relativelayout> </linearlayout>
and group_indicator.xml file:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/arrow1" android:state_expanded="true" /> <item android:drawable="@drawable/arrow2" /> </selector>
how have rewrite code works? i'm new android studio.
Comments
Post a Comment