android - How do I get a specific Activity to show up in a Scrollable Tabs? -
so, i've started experimenting scrollable tabs + swipe navigation type. managed change activity inflate, same activity being shown on tabs.
how make there's different activity on each of tabs? i'm newbie @ android development, please explain easy.
i using default template that's included in eclipse.
thanks in advance!
the mainactivity.java:
package com.heavyfork.slidetest; import java.util.locale; 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.app.navutils; import android.support.v4.view.viewpager; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class mainactivity extends fragmentactivity { /** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use * {@link android.support.v4.app.fragmentpageradapter} derivative, * keep every loaded fragment in memory. if becomes memory * intensive, may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ sectionspageradapter msectionspageradapter; /** * {@link viewpager} host section contents. */ viewpager mviewpager; @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. msectionspageradapter = new sectionspageradapter( getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } /** * {@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) { // getitem called instantiate fragment given page. // return dummysectionfragment (defined static inner class // below) page number lone argument. fragment fragment = new dummysectionfragment(); bundle args = new bundle(); args.putint(dummysectionfragment.arg_section_number, position + 1); fragment.setarguments(args); return fragment; } @override public int getcount() { // show 3 total pages. return 8; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); switch (position) { case 0: return getstring(r.string.preparations).touppercase(l); case 1: return getstring(r.string.title_section1).touppercase(l); case 2: return getstring(r.string.title_section2).touppercase(l); case 3: return getstring(r.string.title_section3).touppercase(); } return null; } } /** * dummy fragment representing section of app, * displays dummy text. */ public static class dummysectionfragment extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public dummysectionfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.activity_picture_display, container, false); return rootview; } } }
you newbie, i'll try explain how sample project works. first of all, talk activity, need use fragment. activity host 1 or more fragments.
the activity diplays viewpager used display fragment (or view) , enable swiping 1 another. sectionspageradapter works adapter objects in android, create one, give data (ie : arraylist of country), implement methods including getview or in case getitem method. in these method given position in viewpager, can access right data , give fragment via bundle object (it's done in example, position +1).
to give data, use :
visufragment fragment = new visufragment(); bundle args = new bundle(); args.putint("id", id); fragment.setarguments(args); return fragment;
and oncreate method of fragment :
int id = getarguments().getint("id");
the viewpager , adpater manage on screen, ask new fragment , delete old ones without knowing.
for sample code, if want see different on each page, delete content of oncreateview method , put :
textview tv = new textview(getactivity().getapplicationcontext()); tv.settext("this fragment number : "+getarguments().getint(dummysectionfragment.arg_section_number); return tv;
Comments
Post a Comment