android - ViewPager Indicator with custom ListView -
first, apologise english since it's not good. thanks. application contains few listwiews adapters. data listviews taken parsed xml file located in internet. have 1 tabhost 3 tabs , different listviews in each tab. want change tabhost new , slighty method - viewpager indicator jake wharton (https://github.com/jakewharton/android-viewpagerindicator), 'cause there awsome sliding effect.
i tried include code in sample project , it's running good. have listview in 1 fragment of viewpager , simple textview in 2 others fragments. it's working.
code of viewpageradapter shown below:
package com.formatbce.pager; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import android.content.context; import android.os.parcelable; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.view.view; import android.widget.abslistview; import android.widget.abslistview.onscrolllistener; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; import com.viewpagerindicator.titleprovider; public class viewpageradapter extends pageradapter implements titleprovider { private int[] scrollposition = new int[titles.length]; private static string[] titles = new string[] { "news", "bio", "music" }; private final context context; public viewpageradapter( context context ) { this.context = context; ( int = 0; < titles.length; i++ ) { scrollposition[i] = 0; } } @override public string gettitle( int position ) { return titles[ position ]; } @override public int getcount() { return titles.length; } @override public object instantiateitem( view pager, final int position ) { if (position == 0) { listview v = new listview( context ); string[] = new string[] { "str" }; int[] = new int[] { android.r.id.text1 }; list<map<string, string>> items = new arraylist<map<string, string>>(); ( int = 0; < 20; i++ ) { map<string, string> map = new hashmap<string, string>(); map.put( "str", string.format( "item %d", + 1 ) ); items.add( map ); } simpleadapter adapter = new simpleadapter( context, items, android.r.layout.simple_list_item_1, from, ); v.setadapter( adapter ); ( (viewpager) pager ).addview( v, 0 ); v.setonscrolllistener( new onscrolllistener() { @override public void onscroll( abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount ) { scrollposition[ position ] = firstvisibleitem; } @override public void onscrollstatechanged(abslistview view, int scrollstate) { // todo auto-generated method stub } } ); return v;} else { textview v = new textview( context ); v.settext( titles[ position ] ); ((viewpager)pager).addview( v, 0 ); return v; } } @override public void destroyitem( view pager, int position, object view ) { if (position == 0){ ( (viewpager) pager ).removeview( (listview) view );} else { ( (viewpager) pager ).removeview( (textview) view );} } @override public boolean isviewfromobject( view view, object object ) { return view.equals( object ); } @override public void finishupdate( view view ) {} @override public void restorestate( parcelable p, classloader c ) {} @override public parcelable savestate() { return null; } @override public void startupdate( view view ) {} }
so, sample listview good.
but problems starting, when try put there custom listview. in old project listview has code:
package com.formatbce.mdrive; import java.util.arraylist; import android.app.alertdialog; import android.app.listactivity; import android.app.progressdialog; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.os.asynctask; import android.os.bundle; import android.view.view; import android.widget.listview; public class concerts extends listactivity { private arraylist<rss_postitem> messages; private rss_postadapter adapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (savedinstancestate != null) { messages = savedinstancestate.getparcelablearraylist("messages"); adapter = new rss_postadapter(concerts.this, r.layout.post_entry, messages); setlistadapter(adapter); } else new getparserresult().execute(); } @override protected void onlistitemclick(listview l, view v, int position, long id) { super.onlistitemclick(l, v, position, id); intent postactivity = new intent(this, rss_postactivity.class); bundle store = new bundle(); store.putstring("title", messages.get(position).title); store.putstring("description", messages.get(position).description); store.putstring("link", messages.get(position).link); postactivity.putextras(store); startactivity(postactivity); } protected void onsaveinstancestate(bundle savedinstancestate) { super.onsaveinstancestate(savedinstancestate); savedinstancestate.putparcelablearraylist("messages", messages); } private class getparserresult extends asynctask<context, integer, arraylist<rss_postitem>> { private progressdialog loadingdialog; private concerts_parser parser; protected void onpreexecute() { parser = new concerts_parser(); loadingdialog = progressdialog.show(concerts.this, "", "loading concerts list...", true); } @override protected arraylist<rss_postitem> doinbackground(context... arg0) { return parser.parse(); } protected void onpostexecute(arraylist<rss_postitem> result) { if (result == null) { alertdialog.builder builder = new alertdialog.builder( concerts.this); builder.setmessage( "cannot load list... \nis internet on?") .setcancelable(false) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { dialog.cancel(); loadingdialog.dismiss(); } }); alertdialog alert = builder.create(); alert.show(); } else { messages = result; loadingdialog.dismiss(); adapter = new rss_postadapter(concerts.this, r.layout.post_entry, messages); setlistadapter(adapter); } } } }
so don't understand, how put correctly in viewpager code. tried set listfragment, it's pretty new me, can't working.
google can't me too, same various developer forums.
truly, question is: how custom listview shown in viewpager?
thank you, i'm hope you're help.
upd:
thanks first answer, tried convert 1 of activities fragment tutorial: http://www.e-nature.ch/tech/?p=55.
first @ all, 1 error couldn't pass: variable isn't converted, , don't know, how working.
dm = (downloadmanager) getsystemservice(download_service);
now 1 other thing: when tried put result fragment fragmentpageradapter - stuck finding correct code here:
@override public fragment getitem(int arg0) { // todo auto-generated method stub return ?????????; }
tell me, please, method have return? thanks!
you said
i have 1 tabhost 3 tabs, , different listview in each tab
i assume u have 3 different listactivity , passed tabhost. convert 3 activity 3 fragments.
based on folllowing sample code change viewpageradapter
class googlemusicadapter extends fragmentpageradapter implements titleprovider { public googlemusicadapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { if(position ==0 ) // return fragment1 else if(position==1) // return fragment2 else // return fragment3 } @override public int getcount() { return 3; } @override public string gettitle(int position) { if(position ==0 ) return "one" else if(position ==1) return "two" else return "three" } }
Comments
Post a Comment