android - ListView items don't load on screen -
the use case following: want load listview inside viewpager , @ same time set onitemclicklistener on list.
can tell why listview doesn't load, i.e. screen stays black. not strip pager works.
layout:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="4dip" > <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="0px" > <android.support.v4.view.pagertitlestrip android:id="@+id/pager_title_strip" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:layout_weight="1" android:background="#33b5e5" android:paddingbottom="4dp" android:paddingtop="4dp" android:textcolor="#fff" /> <listview android:id="@+id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:drawselectorontop="false" /> </android.support.v4.view.viewpager>
some code:
public static class headerfragment extends listfragment { list<message> messages = new arraylist<message>(); string topic; public headerfragment(list<message> messages, string topic) { this.messages = messages; this.topic = topic; } public headerfragment() { // todo auto-generated constructor stub } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.main, container, false); final listview listview = (listview) v.findviewbyid(android.r.id.list); final messageitemadapter adapter = new messageitemadapter(inflater, r.layout.headers, messages, 1); //the click listener. listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { message message = adapter.getitem(position); intent intent = new intent(); intent.setclassname( "main", "newsactivity"); intent.putextra("title", message.gettitle()); intent.putextra("description", message.getdescription()); intent.putextra("source", message.getlink().tostring()); intent.putextra("language", location); startactivity(intent); } }); listview.setadapter(adapter); return v; }
thanks in advance !!!
edit:
the code calls fragment:
public class headersactivitypageradapter extends fragmentstatepageradapter { public headersactivitypageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int i) { int k = 0; string topic = null; list<message> messages = new arraylist<message>(); (entry<string, list<message>> elem : fragmentsdata.entryset()) { if (k==i) { topic = elem.getkey(); messages = elem.getvalue(); break; } k++; } return new headerfragment(messages, topic); } @override public int getcount() { return fragmentsdata.size(); } @override public charsequence getpagetitle(int position) { int k = 0; string topic = ""; (entry<string, list<message>> elem : fragmentsdata.entryset()) { if (k==position) { topic = elem.getkey(); break; } k++; } return topic; } }
it works this, lose listener listview:
main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="4dip" > <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="1" > <android.support.v4.view.pagertitlestrip android:id="@+id/pager_title_strip" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="#33b5e5" android:paddingbottom="4dp" android:paddingtop="4dp" android:textcolor="#fff" /> </android.support.v4.view.viewpager>
submain.xml
<?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" > <framelayout android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" > <listview android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:drawselectorontop="false" /> </framelayout>
some code:
public static class headerfragment extends listfragment { list<message> messages = new arraylist<message>(); string topic; public headerfragment(list<message> messages, string topic) { this.messages = messages; this.topic = topic; } public headerfragment() { // todo auto-generated constructor stub } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.submain, container, false); final listview listview = (listview) v.findviewbyid(android.r.id.list); final messageitemadapter adapter = new messageitemadapter(inflater, r.layout.headers, messages, 1); //the click listener. listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { message message = adapter.getitem(position); intent intent = new intent(); intent.setclassname( "main", "newsactivity"); intent.putextra("title", message.gettitle()); intent.putextra("description", message.getdescription()); intent.putextra("source", message.getlink().tostring()); intent.putextra("language", location); startactivity(intent); } }); listview.setadapter(adapter); return v; }
the problem lose listener, doesn't trigger. solution found slows hell down application.
i place listener inside adapter, each item.
your viewpager set height 0px
. give larger height , should able see it. perhaps:
<android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" >
edit:
show code creating fragment , adding screen. add logging constructor:
public headerfragment(list<message> messages, string topic) { this.messages = messages; log.i("tag", "messages size: " + this.messages.size(); // <- add this.topic = topic; }
to ensure messages
has items in when gets passed here. fragment never adds items messages
if has none in when create fragment there won't show in listview.
Comments
Post a Comment