java - View Pager Android -
i'm trying implement view pager : http://developer.android.com/training/animation/screen-slide.html
but seem have problem when comes creating page number. when debugging downloaded zip previous mentioned website, first method called create(int pagenumber) , afterwards oncreate() you're getting page number. in case, it's other way around, therefore null pointer exception.
here current implementation of view pager:
public class singlecheckindisplay extends android.support.v4.app.fragment { private checkin data; private fragmentmanager fragmentmanager; private list<checkinuser> enlooped; private textview checkinlocation; private textview checkindescription; private textview checkintime; private button cancelbtn; private imagebutton singlecheckinenloopbtn; private imagebutton singlecheckincancelbtn; private horizontallistview enloopedfriends; public static final string arg_page = "page"; private int mpagenumber; public static singlecheckindisplay create(int pagenumber) { singlecheckindisplay fragment = new singlecheckindisplay(); bundle args = new bundle(); args.putint(arg_page, pagenumber); fragment.setarguments(args); return fragment; } public singlecheckindisplay(checkin data) { this.data = data; } public singlecheckindisplay() { } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mpagenumber = 2; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_single_check_in_display, container, false); enlooped = data.getcheckinusers(); fragmentmanager = getactivity().getsupportfragmentmanager(); imageview main_pic = (imageview) v.findviewbyid(r.id.main_pic); checkinlocation = (textview) v.findviewbyid(r.id.single_check_in_location); checkindescription = (textview) v.findviewbyid(r.id.single_check_in_desc); checkintime = (textview) v.findviewbyid(r.id.single_check_in_time_text); checkintime.settext(data.getcheckindate().tostring()); enloopedfriends = (horizontallistview) v.findviewbyid(r.id.sinlge_check_in_enlooped_list); displayimageoptions options = new displayimageoptions.builder() .showimageonloading(r.drawable.sm_profile) .showimageforemptyuri(r.drawable.sm_profile) .showimageonfail(r.drawable.sm_profile) .cacheondisk(true) .cacheinmemory(true) .imagescaletype(imagescaletype.exactly) .considerexifparams(true) .displayer(new simplebitmapdisplayer()) .build(); imageloader.getinstance().displayimage(data.getimages(), main_pic, options); checkindescription.settext(data.getdescription()); checkinlocation.settext(data.getplaceaddressandname()); checkinlocation.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // set bundle place id, rest obtained calling graph api bundle args = new bundle(); args.putstring("placeid", data.getplaceid()); singlecheckinplace scp = new singlecheckinplace(); scp.setarguments(args); fragmentmanager fm = getactivity().getsupportfragmentmanager(); fm.begintransaction().replace(r.id.container, scp).addtobackstack(null).commit(); } }); singlecheckinadapter adapter = new singlecheckinadapter(getactivity(), enlooped); enloopedfriends.setadapter(adapter); cancelbtn = (button) v.findviewbyid(r.id.single_ck_display_cancel_button); cancelbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mapsfilterfragment firstfragment = new mapsfilterfragment(); fragmentmanager.begintransaction().add(r.id.container, firstfragment).commit(); } }); singlecheckinenloopbtn = (imagebutton) v.findviewbyid(r.id.single_check_in_enloop_btn); singlecheckinenloopbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(getactivity(), "enlooped !", toast.length_short).show(); fragmentmanager.popbackstack(); } }); singlecheckincancelbtn = (imagebutton) v.findviewbyid(r.id.single_check_in_no_btn); singlecheckincancelbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(getactivity(), "maybe next time", toast.length_short).show(); goback(); } }); return v; } @override public void onattach(activity activity) { super.onattach(activity); } @override public void ondetach() { super.ondetach(); } private void goback() { fragmentmanager.popbackstack(); } public int getpagenumber() { return mpagenumber; } }
and here miplementation of pager itself:
public class screenslidefragment extends android.support.v4.app.fragment { private viewpager mpager; private pageradapter mpageradapter; private static final int num_pages = 11; public screenslidefragment() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_screen_slide, container, false); mpager = (viewpager) view.findviewbyid(r.id.pager); mpageradapter = new screenslidepageradapter(getfragmentmanager()); mpager.setadapter(mpageradapter); return view; } private class screenslidepageradapter extends fragmentstatepageradapter { public screenslidepageradapter(android.support.v4.app.fragmentmanager fm) { super(fm); } @override public android.support.v4.app.fragment getitem(int position) { return singlecheckindisplay.create(position); } @override public int getcount() { return num_pages; } } }
do this
viewpager mviewpager=(viewpage)findviewbyid(r.id.urcontainer); mviewpager.addonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpageselected(int position) { int page_number=position; //this give u current page number } });
`
Comments
Post a Comment