android - how to move to new fragment at on OnItemClickListener() -
in application want put listview in first fragment , want move new fragment when clicked on item such each item has own details in code.
i implemented move activity, manager says must go fragment instead of other activity. i'm new fragment world , don't know how this. manager says can use list fragment have not found useful code.
this code:
public class mainactivity extends fragmentactivity { sectionspageradapter msectionspageradapter; static progressdialog pd ; viewpager mviewpager; list<fragment> fragments ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // create adapter return fragment each of 3 // primary sections of app. fragments = new vector<fragment>(); fragments.add(fragment.instantiate(this, fragmentone.class.getname())); fragments.add(fragment.instantiate(this, fragmenttwo.class.getname())); fragments.add(fragment.instantiate(this, fragmentthree.class.getname())); fragments.add(fragment.instantiate(this, fragmentfour.class.getname())); fragments.add(fragment.instantiate(this, fragmentfive.class.getname())); fragments.add(fragment.instantiate(this, fragmentsix.class.getname())); msectionspageradapter=new sectionspageradapter(super.getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); pd = new progressdialog(this); mviewpager.setadapter(msectionspageradapter); // } public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { int _pos = position % 6; return fragments.get(_pos); } @override public int getcount() { // show 3 total pages. return 6; } @override public charsequence getpagetitle(int position) { final string title_section4="section4"; final string title_section5="section5"; final string title_section6="section6"; final string title_section1="section1"; final string title_section2="section2"; final string title_section3="section3"; locale l = locale.getdefault(); switch (position) { case 0: return title_section1.touppercase(l); case 1: return title_section2.touppercase(l); case 2: return title_section3.touppercase(l); case 3: return title_section4.touppercase(l); case 4: return title_section5.touppercase(l); case 5: return title_section6.touppercase(l); } return null; } } public static class fragmentone extends fragment { arraylist< string > ar; arrayadapter< string > ad ; listview lv ; textview tv; public fragmentone() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.one, container, false); tv = (textview) rootview.findviewbyid(r.id.mywidget); tv.setselected(true); ar = new arraylist<string>(); lv = (listview) rootview.findviewbyid(r.id.listview1); (int = 0 ; i< 10 ; i++){ ar.add("my item " + string.valueof(i)); } ad = new arrayadapter<string> (getactivity().getapplicationcontext(), android.r.layout.simple_dropdown_item_1line, ar); lv.setadapter(ad); lv.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast t = toast.maketext(getactivity(), "message", toast.length_short); t.show(); intent = new intent(getactivity(), tst.class); startactivity(i); } }); return rootview; } } public static class fragmenttwo extends fragment { public fragmenttwo() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.two, container, false); return rootview; } } public static class fragmentthree extends fragment { public fragmentthree() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.three, container, false); return rootview; } } public static class fragmentfour extends fragment { public fragmentfour() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.four, container, false); return rootview; } } public static class fragmentfive extends fragment { public fragmentfive() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.five, container, false); return rootview; } } public static class fragmentsix extends fragment { public fragmentsix() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.six, container, false); return rootview; } } }
you have replace fragment using transaction
see here:
http://developer.android.com/guide/components/fragments.html#example
check showdetails(int index) method of titlesfragment class:
fragmenttransaction ft = getfragmentmanager().begintransaction(); if (index == 0) { ft.replace(r.id.details, details); } else { ft.replace(r.id.a_item, details); } ft.settransition(fragmenttransaction.transit_fragment_fade); ft.commit();
Comments
Post a Comment