Android Studio creating ViewPager -
i have button on mainactivity opens secondactivity. secondactivity viewpager, , have 2 fragment classes (fragment1, fragment2) , 2 fragment xml, each textview inside. cannot work out how put 2 fragment inside viewpage using fragmentpageradapter can slide 1 fragment another. can please show/help me?
mainactivity
button button1 = (button) findviewbyid(r.id.buttoncreateworkout); button1.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { startactivity(new intent(mainactivity.this, secondactivity.class)); }
second_activity.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> // //how add 2 fragments here? //if right place // </android.support.v4.view.viewpager> </linearlayout>
also, able show me how use fragmentpageradapter switch between 2 fragments?
dont add fragments inside de viewpager xml tag, add fragments code.fragmentpageradapter handle this. should follow tutorial correct implementation: create swype viewa
the pagerfragmentadapter should this, adapter handle fragment creation , switch fragment:
public class democollectionpageradapter extends fragmentstatepageradapter { public democollectionpageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int i) { fragment fragment = new demoobjectfragment(); bundle args = new bundle(); // our object integer :-p args.putint(demoobjectfragment.arg_object, + 1); fragment.setarguments(args); return fragment; } @override public int getcount() { return 100; } @override public charsequence getpagetitle(int position) { return "object " + (position + 1); }
}
inside getitem method, create fragment depending on value of index.
Comments
Post a Comment