performance - Making dynamic layout for each row in ListView in android -
i have xml file contains basic layout each row of listview(which realtive layout , has textview inside it).
i want change attributes of layout each row of listview different layout width , height of each row. want set values of width , height dynamically.
is there way around this?
my xml file want change, height , weight dynamically, each view
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:id="@+id/item1" android:layout_width="wrap_content"> <textview android:id="@+id/text" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="text" android:visibility="visible" android:layout_width="wrap_content" android:textcolor="#ff200010" android:background="#fffcccff" /> </linearlayout>
and full file
package com.test.list; import java.util.arraylist; import java.util.treeset; import android.app.listactivity; import android.content.context; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.linearlayout; import android.widget.textview; public class multipleitemslist extends listactivity { private mycustomadapter madapter; public context context =getapplicationcontext(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); madapter = new mycustomadapter(); (int = 1; < 50; i++) { madapter.additem("item " + i); if (i % 4 == 0) { madapter.addseparatoritem("separator " + i); } } setlistadapter(madapter); } private class mycustomadapter extends baseadapter { private static final int type_item = 0; private static final int type_separator = 1; private static final int type_max_count = type_separator + 1; private arraylist<string> mdata = new arraylist<string>(); private layoutinflater minflater; private treeset<integer> mseparatorsset = new treeset<integer>(); public mycustomadapter() { minflater = (layoutinflater)getsystemservice(context.layout_inflater_service); } public void additem(final string item) { mdata.add(item); notifydatasetchanged(); } public void addseparatoritem(final string item) { mdata.add(item); // save separator position mseparatorsset.add(mdata.size() - 1); notifydatasetchanged(); } @override public int getitemviewtype(int position) { return mseparatorsset.contains(position) ? type_separator : type_item; } @override public int getviewtypecount() { return type_max_count; } @override public int getcount() { return mdata.size(); } @override public string getitem(int position) { return mdata.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; int type = getitemviewtype(position); system.out.println("getview " + position + " " + convertview + " type = " + type); if (convertview == null) { holder = new viewholder(); switch (type) { case type_item: convertview = minflater.inflate(r.layout.item1,null); log.e("ronak", "here1"); textview t= (textview)convertview.findviewbyid(r.id.text); t.setwidth(100); t.setheight(600); t.settext("this first type of view"); holder.textview = (textview)convertview.findviewbyid(r.id.text); log.e("ronak","reached here3"); break; case type_separator: convertview = minflater.inflate(r.layout.item1, null); textview t2= (textview)convertview.findviewbyid(r.id.text); t2.setwidth(200); t2.setheight(500); t2.settext("this second type of view"); holder.textview = (textview)convertview.findviewbyid(r.id.text); break; } convertview.settag(holder); } else { holder = (viewholder)convertview.gettag(); } holder.textview.settext(mdata.get(position)); return convertview; } } public static class viewholder { public textview textview; } }
you can implement viewholder
pattern adapter, , each position, inflate own layout.
to that, override getview
this:
public view getview(int position, view convertview, viewgroup parent) { // viewholder keeps references children views avoid unneccessary calls // findviewbyid() on each row. viewholder holder; // when convertview not null, can reuse directly, there no need // reinflate it. inflate new view when convertview supplied // listview null. if (convertview == null) { convertview = minflater.inflate(r.layout.sample, null); // creates viewholder , store references 2 children views // want bind data to. holder = new viewholder(); holder.name = (textview) convertview.findviewbyid(r.id.text); holder.icon = (imageview) convertview.findviewbyid(r.id.icon); convertview.settag(holder); } else { // viewholder fast access textview // , imageview. holder = (viewholder) convertview.gettag(); } // bind data efficiently holder. holder.name.settext(myelements.get(id)); holder.icon.setimagebitmap( micon1 ); return convertview; }
read more on android developer site here
edit:
to change width , height of each row, can use following, each row on bind data section :
relativelayout rl = (relativelayout) findviewbyid(r.id.yourid); rl.getlayoutparams().height = 100; rl.getlayoutparams().width = 100;`
Comments
Post a Comment