The effect of--fragment and viewpager of rabbits on switching and skidding

Source: Internet
Author: User


Directory structure:


Code Analysis:

Mainactivity.java

Package com.example.myfragment;/** * @author Arthur Lee * @time 04/08/2014 * */import java.util.arraylist;import ANDROID.O S.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.linearlayout;import Android.widget.radiobutton;import Com.fragment.firstfragment;import Com.fragment.fourthfragment;import Com.fragment.secondfragment;import Com.fragment.thirdfragment;public Class Mainactivity extends fragmentactivity {arraylist<fragment> fragment;public static Viewpager viewpager;public Static arraylist<radiobutton> RB; LinearLayout llayout;//Viewpager Adapter, but in order to combine with fragment, to customize the adapter Myadapter adapter; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); inItview (); Initbutton (); Initadapter ();} public void Initview () {Viewpager = (Viewpager) Findviewbyid (r.id.main_viewpager); llayout = (linearlayout) findViewById (R.ID.ZONE_RBT); fragment = new arraylist<fragment> (); Fragment.add (new Firstfragment ()); Fragment.add (new Secondfragment ()); Fragment.add (new Thirdfragment ()); Fragment.add (new Fourthfragment ());} public void Initadapter () {adapter = new Myadapter (Getsupportfragmentmanager (), fragment); Viewpager.setadapter ( adapter); Viewpager.setonpagechangelistener (myonpagechangelistener);//page displayed by default Viewpager.setcurrentitem (0); rb.get (0). Setchecked (true);} public void Initbutton () {RB = new arraylist<radiobutton> (); Rb.add ((RadioButton) Findviewbyid (R.id.rb_first)); /No. 1th Rb.add ((RadioButton) Findviewbyid (R.id.rb_second));//2nd Rb.add ((RadioButton) Findviewbyid (R.id.rb_third));// No. 3rd Rb.add ((RadioButton) Findviewbyid (R.id.rb_fourth));//4th for (int i = 0; i < rb.size (); i++) {Rb.get (i). Setonclicklis Tener (New MyListener (i));}} public class MyListenerImplements Onclicklistener {private int index;public mylistener (int index) {this.index = index;} @Overridepublic void OnClick (View v) {viewpager.setcurrentitem (index); rb.get (index). setchecked (True);}} Onpagechangelistener Myonpagechangelistener = new Onpagechangelistener () {//page card status changed. @Overridepublic void onpagescrollstatechanged (int arg0) {log.i ("Main", "onpagescrollstatechanged");} Page card switching process @overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {log.i ("Main", "onpagescrolled");} When RadioButton is selected @overridepublic void onpageselected (int position) {log.i ("Main", "onpageselected"); Rb.get (position ). Setchecked (True);}};}

Myadapter.java

Package Com.example.myfragment;import Java.util.arraylist;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmentpageradapter;public class MyAdapter Extends Fragmentpageradapter {private arraylist<fragment> fragment;public Myadapter (fragmentmanager FM) {Super ( FM);//TODO auto-generated constructor stub}public myadapter (Fragmentmanager FM, arraylist<fragment> Fragment) { Super (FM);//TODO auto-generated constructor stubthis.fragment = fragment;} @Overridepublic Fragment getItem (int position) {//TODO auto-generated method Stubreturn fragment.get (position);} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn fragment.size ();}}


Firstfragment.java

Package com.fragment;/** * @author Arthur Lee * @time 04/08/2014 * */import com.example.myfragment.r;import Android.os.Bun Dle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup;public class Firstfragment extends fragment{    //Cache view private view view; @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate);}    @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {//TODO Auto-generated method stub//If the current view is empty, initialize the view if (view = = null) {//Specifies that the current view displays View_first in Viewpager. XML, specified by layoutinflater. View = inflater.inflate (R.layout.view_first, null);} Specifies the parent class of the current view in order to invoke the Remove function of the parent class.    ViewGroup parent = (ViewGroup) view.getparent ();    if (parent! = null) {Parent.removeview (view);} return view;}}

The other three are similar to this one and are no longer available.

Here is the layout file

Main_activity.xml

<linearlayout 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:orien tation= "Vertical" tools:context= ". Mainactivity "> <!--vertical Two, with AB markers---!--A--and <linearlayout android:layout_width=" mat Ch_parent "android:layout_height=" Match_parent "android:layout_weight=" 1 "> <android.support.v4 . View. Viewpager android:id= "@+id/main_viewpager" android:layout_width= "Match_parent" Android:la yout_height= "Match_parent" > </android.support.v4.view.ViewPager> </LinearLayout> <!--B-- > <linearlayout android:id= "@+id/zone_rbt" android:layout_width= "Match_parent" android:layou t_height= "Match_parent" android:layout_weight= "6" android:orientation= "Horizontal" > <radiogrou   P         Android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:gravity= "C Enter "android:orientation=" horizontal "> <radiobutton android:id=" @+id/rb_firs T "android:layout_width=" match_parent "android:layout_height=" Match_parent "a ndroid:layout_weight= "1" android:background= "@android: Color/holo_blue_light" android:text= "1 Number "/> <radiobutton android:id=" @+id/rb_second "android:layout_width=" Match_ Parent "android:layout_height=" Match_parent "android:layout_weight=" 1 "Androi                D:background= "@android: Color/holo_green_light" android:text= "No. 2nd"/> <radiobutton Android:id= "@+id/rb_third" android:layout_width= "Match_parent" android:layout_height= "           Match_parent "     android:layout_weight= "1" android:background= "@android: Color/holo_blue_light" android:t Ext= "No. 3rd"/> <radiobutton android:id= "@+id/rb_fourth" android:layout_width= "                Match_parent "android:layout_height=" Match_parent "android:layout_weight=" 1 "    Android:background= "@android: Color/holo_green_light" android:text= "No. 4th"/> </RadioGroup> </LinearLayout></LinearLayout>

View_first.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:o rientation= "Vertical" >    <!--@author Arthur Lee--    <textview        android:id= "@+id/view_first"        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:text= "first page"        Android:textsize= "20SP"/></linearlayout>


The other three layout files are similar to this one and are no longer available.

The effect of--fragment and viewpager of rabbits on switching and skidding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.