android - Duplicated elements when dynamic loading them in horizontal scrollview -
i have main.xml have listview. row of listview defined in row.xml this:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="80dp" android:orientation="horizontal" > <imageview android:id="@+id/image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="60" android:contentdescription="@string/app_name" android:src="@drawable/ic_launcher" /> <textview android:id="@+id/text" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="40" android:gravity="center" android:textcolor="#ff0000" android:text="sdfasdf" /> </linearlayout> <horizontalscrollview android:layout_width="match_parent" android:background="#ffda35" android:layout_height="70dp" > <linearlayout android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > </linearlayout> </horizontalscrollview>
as can see, there horizontal scroll view in every listview row , when listview generated row, want dynamic loading scrollview children in it. scrollview child defined in scrollview_child.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="60dp" android:layout_height="match_parent" android:background="@null" > <relativelayout android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginbottom="5dp" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:layout_margintop="5dp" android:background="#ffffff" > <imageview android:id="@+id/img" android:layout_width="40dp" android:layout_height="40dp" android:contentdescription="@string/app_name" android:gravity="center" android:src="@drawable/ic_launcher" /> <textview android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignleft="@+id/img" android:layout_alignright="@+id/img" android:layout_below="@+id/img" android:gravity="center" /> </relativelayout>
my java codes: mainactivity.java
public class mainactivity extends actionbaractivity { listview list; private arraylist<string> people; private arraylist<string> school; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); list = (listview)findviewbyid(r.id.list); createlists(); listviewadapter listadapter = new listviewadapter(this, people, school); list.setadapter(listadapter); } public void createlists() { people = new arraylist<string>(); people.add("bob"); people.add("peter"); people.add("anna"); people.add("jane"); school = new arraylist<string>(); school.add("a"); school.add("b"); school.add("c"); school.add("d"); school.add("e"); }
}
listviewadapter.java:
public class listviewadapter extends baseadapter{ private arraylist<string> people; private arraylist<string> school; mainactivity activity; public listviewadapter(mainactivity activity, arraylist<string> people, arraylist<string> school) { this.people = people; this.school = school; this.activity = activity; } private static class viewholder { private textview catename; private imageview cateimage; private linearlayout ll; private viewholder (view v) { cateimage = (imageview)v.findviewbyid(r.id.image); catename = (textview)v.findviewbyid(r.id.text); ll = (linearlayout)v.findviewbyid(r.id.ll); } } @override public int getcount() { // todo auto-generated method stub return people.size(); } @override public object getitem(int position) { return people.get(position); } @override public long getitemid(int position) { return people.indexof(getitem(position)); } @suppresslint("inlinedapi") @override public view getview(int position, view convertview, viewgroup parent) { log.i("run", "run"); viewholder holder = null; layoutinflater infllater = (layoutinflater) activity.getsystemservice(activity.layout_inflater_service); if (convertview == null) { convertview = infllater.inflate(r.layout.home_page_list_odd_item, parent, false); holder = new viewholder(convertview); convertview.settag(holder); } else { holder = (viewholder)convertview.gettag(); } // 1 row element holder.cateimage.setimageresource(r.drawable.defaultavatar); holder.catename.settext(people.get(position)); log.i("size: ", "school: " + school.size() + "people: " + people.size()); // set data scroll view (int = 0; i<school.size(); i++) { layoutinflater inflater = (layoutinflater) this.activity.getsystemservice(context.layout_inflater_service); view v = inflater.inflate(r.layout.home_page_scroll_list_item, parent, false); textview schoolname = (textview) v.findviewbyid(r.id.name); imageview schoolimage = (imageview)v.findviewbyid(r.id.img); relativelayout rl = (relativelayout)v.findviewbyid(r.id.rl); schoolname.settext(school.get(i)); schoolimage.setimageresource(r.drawable.ic_launcher); rl.setbackgroundresource(r.drawable.bg_deal); holder.ll.addview(v); } return convertview; }
}
my problem children in horizontal scrollview duplicated because method getview() calling multi time. result this: http://i.imgur.com/70tte3i.png
please help! sorry english badly!
Comments
Post a Comment