android - Viewpager load fragments 1 by 1 -
i have few tabs in app each of them have big network calls it's not practical load them @ starting application. how can implement thing load fragments 1 1 when user switch tab load etc.
i tried setoffscreenpagelimit(1) it's not working.
below complete code add fragments view pager. since demonstration have used textview , each fragment added have random colour.on clicking button can add many fragments want.on clicking page can remove view pager.you can move 1 page in view pager swiping right or left .
mainactivity.java
import android.graphics.color; 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.fragmentstatepageradapter; import android.support.v4.view.viewpager; import android.view.view; import android.widget.button; import java.util.arraylist; import java.util.random; public class mainactivity extends fragmentactivity { private mypageradapter mpg; private arraylist<myfragment> fragmentlist = new arraylist<>(); public fragmentmanager fmr = getsupportfragmentmanager(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final viewpager pager = (viewpager) findviewbyid(r.id.viewpager); mpg = new mypageradapter(fmr,fragmentlist); pager.setadapter(mpg); (int q = 0; q < 5; q++) { int r = new random().nextint(100) + 155; int g = new random().nextint(100) + 155; int b = new random().nextint(100) + 155; fragmentlist.add(myfragment.newinstance(q, "page number: " + (q + 1), color.rgb(r, g, b))); mpg.notifydatasetchanged(); } button b1 = (button) findviewbyid(r.id.button); b1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int r = new random().nextint(100) + 155; int g = new random().nextint(100) + 155; int b = new random().nextint(100) + 155; int count = mpg.getcount(); fragmentlist.add(myfragment.newinstance(count, "page number: " + (count+1), color.rgb(r, g, b))); mpg.notifydatasetchanged(); } }); } public void delete(int pos) { int = 0; while (i <= fragmentlist.size()) { if (pos == fragmentlist.get(i).getposition()) break; i++; } fragmentlist.remove(i); mpg.notifydatasetchanged(); } public class mypageradapter extends fragmentstatepageradapter { private arraylist<myfragment> fragmentlist; public mypageradapter(fragmentmanager fm, arraylist<myfragment> f) { super(fm); this.fragmentlist = f; } @override public fragment getitem(int pos) { return fragmentlist.get(pos); } @override public int getcount() { return fragmentlist.size(); } @override public int getitemposition(object object) { return position_none; } } }
myfragment.java
import android.os.bundle; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.framelayout; import android.widget.textview; import java.util.list; public class myfragment extends fragment { int pos; @override public view oncreateview(layoutinflater inflater, final viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.f_1, container, false); pos = getarguments().getint("num"); v.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.v("mainactivity", "deleting @ pos - " + pos); mainactivity activity = (mainactivity) getactivity(); activity.delete(pos); } }); textview tv = (textview) v.findviewbyid(r.id.tvfragfirst); framelayout fl = (framelayout) v.findviewbyid(r.id.frame_layout); tv.settext(getarguments().getstring("msg")); fl.setbackgroundcolor(getarguments().getint("colour")); return v; } public int getposition() { return pos; } public static myfragment newinstance(int num,string text,int clr) { myfragment f = new myfragment(); bundle b = new bundle(); b.putstring("msg", text); b.putint("colour", clr); b.putint("num",num); f.setarguments(b); return f; } }
and xml.files,
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.shakthivels.assingment10.mainactivity" android:id="@+id/r_v" > <android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add fragment" android:id="@+id/button" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" /> </relativelayout>
f_1.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/av" > <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:id="@+id/frame_layout"> <textview android:id="@+id/tvfragfirst" android:layout_width="203dp" android:layout_height="127dp" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:textsize="26sp" android:text="textview" android:layout_gravity="center" android:textcolor="#000000" /> </framelayout> </relativelayout>
Comments
Post a Comment