Android - Getting context of a fragment in a pager -
i have android application using pager navigation. tabs have 3 layouts content. on 1 of fragments gallery , want add images it. have set imageadapter need know how access context of fragment.
final layoutinflater factory = getlayoutinflater(); final view view = factory.inflate(r.layout.pictures, null); gallery g = (gallery) view.findviewbyid(r.id.gallery1); g.setadapter(new imageadapter(view.getcontext()));
i use code above in oncreate method gallery layout not contentview. have give context imageadapter. context have set there?
edit: complete code:
package com.bw2801.uwelugemediathek; import java.util.locale; import android.app.actionbar; import android.app.fragmenttransaction; import android.content.context; import android.database.datasetobserver; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.baseadapter; import android.widget.gallery; import android.widget.imageview; import android.widget.spinneradapter; import android.widget.toast; public class mainactivity extends fragmentactivity implements actionbar.tablistener { sectionspageradapter msectionspageradapter; viewpager mviewpager; picturessectionfragment ps = new picturessectionfragment(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // set action bar. final actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // create adapter return fragment each of 3 // primary sections of app. msectionspageradapter = new sectionspageradapter( getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); // when swiping between different sections, select corresponding // tab. can use actionbar.tab#select() if have // reference tab. mviewpager .setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); } }); // each of sections in app, add tab action bar. (int = 0; < msectionspageradapter.getcount(); i++) { // create tab text corresponding page title defined // adapter. specify activity object, implements // tablistener interface, callback (listener) when // tab selected. actionbar.addtab(actionbar.newtab() .settext(msectionspageradapter.getpagetitle(i)) .settablistener(this)); } final layoutinflater factory = getlayoutinflater(); final view view = factory.inflate(r.layout.pictures, null); gallery g = (gallery) view.findviewbyid(r.id.gallery1); g.setadapter(new imageadapter(ps.getactivity())); } public class imageadapter extends baseadapter { private context mcontext; private integer[] mimageids = { r.drawable.image01, r.drawable.image02, r.drawable.image03, r.drawable.image04, r.drawable.image05, r.drawable.image06, r.drawable.image07, r.drawable.image08, }; public imageadapter(context c) { mcontext = c; } public int getcount() { return mimageids.length; } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { imageview = new imageview(mcontext); i.setimageresource(mimageids[position]); i.setlayoutparams(new gallery.layoutparams(150, 100)); i.setscaletype(imageview.scaletype.fit_xy); return i; } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { // when given tab selected, switch corresponding page in // viewpager. mviewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { switch(position) { case 0: return new dummysectionfragment(); case 1: return new soundsectionfragment(); case 2: return ps; } return new dummysectionfragment(); } @override public int getcount() { return 3; } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return "informationen"; case 1: return "soundboard"; case 2: return "galerie"; } return null; } } public static class dummysectionfragment extends fragment { public dummysectionfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.info, container, false); } } public static class picturessectionfragment extends fragment { public picturessectionfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.pictures, container, false); } } public static class soundsectionfragment extends fragment { public soundsectionfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.sounds, container, false); } } }
you can use
g.setadapter(new imageadapter(getactivity()));
Comments
Post a Comment