Use of Android Fragmentactivity+viewpager

Source: Internet
Author: User

Use the scene to design a "bottom menu bar + The rest of the sliding page" of the simple features.

 PackageCom.lanyuweng.mibaby;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.support.v4.app.FragmentActivity;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.Window;ImportAndroid.widget.CalendarView;ImportAndroid.widget.TextView;ImportCom.lanyuweng.mibaby.fragment.FragmentAdapter; Public classMainactivityextendsFragmentactivityImplementsonclicklistener{Private Static FinalString TAG = "mainactivity-----";  Public Static Final intTab_calendar = 0;  Public Static Final intTab_reminder = 1;  Public Static Final intTab_note = 2;  Public Static Final intTab_more = 3; PrivateCalendarView CalendarView; PrivateViewpager Viewpager; PrivateTextView Tvcalendar,tvreminder,tvnote,tvmore; //Defining Adapters    PrivateFragmentadapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);  This. Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.activity_main);    Initview (); }    Private voidInitview () {log.i (TAG,"Initview----"); Viewpager=(Viewpager) Findviewbyid (R.id.viewpager); Tvcalendar=(TextView) Findviewbyid (R.id.tvcalendar); Tvreminder=(TextView) Findviewbyid (R.id.tvreminder); Tvnote=(TextView) Findviewbyid (r.id.tvnote); Tvmore=(TextView) Findviewbyid (R.id.tvmore); Tvcalendar.setonclicklistener ( This); Tvreminder.setonclicklistener ( This); Tvnote.setonclicklistener ( This); Tvmore.setonclicklistener ( This); Adapter=NewFragmentadapter (Getsupportfragmentmanager ());    Viewpager.setadapter (adapter); } @Override Public voidOnClick (View arg0) {Switch(Arg0.getid ()) { CaseR.id.tvcalendar:viewpager.setcurrentitem (Tab_calendar);            Findviewbyid (R.id.tvconfig);  Break;  CaseR.id.tvmore:viewpager.setcurrentitem (Tab_more);  Break;  CaseR.id.tvnote:viewpager.setcurrentitem (tab_note);  Break;  CaseR.id.tvreminder:viewpager.setcurrentitem (Tab_reminder);  Break; default:             Break; }} @Overrideprotected voidOnDestroy () {//TODO auto-generated Method Stub        Super. OnDestroy (); }    }

Two ways to switch pages, swipe left and right, and tap the bottom menu bar. The second approach is to use the OnClick () method, and the second is to inherit the Fragmentpageradapter to control the display of the fragment.

 Packagecom.lanyuweng.mibaby.fragment;Importcom.lanyuweng.mibaby.MainActivity;Importandroid.support.v4.app.Fragment;ImportAndroid.support.v4.app.FragmentManager;ImportAndroid.support.v4.app.FragmentPagerAdapter; Public classFragmentadapterextendsFragmentpageradapter { Public Final Static intTab_count = 4;  PublicFragmentadapter (fragmentmanager FM) {Super(FM); } @Override PublicFragment GetItem (intarg0) {        Switch(arg0) { CaseMainActivity.TAB_CALENDAR:CalendarFragment calendarfragment=Newcalendarfragment (); returncalendarfragment;  CaseMainActivity.TAB_REMINDER:ReminderFragment reminderfragment=Newreminderfragment (); returnreminderfragment;  CaseMainActivity.TAB_NOTE:NoteFragment notefragment=Newnotefragment (); returnnotefragment;  CaseMainActivity.TAB_MORE:MoreFragment morefragment=Newmorefragment (); returnmorefragment; }        return NULL; } @Override Public intGetCount () {//TODO auto-generated Method Stub        returnTab_count; }} 

At the same time, XML is:

<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:background= "@drawable/background"android:orientation= "Vertical" > <Android.support.v4.view.ViewPager Android:id= "@+id/viewpager"Android:layout_width= "Fill_parent"Android:layout_height= "Match_parent"Android:layout_weight= "9"/> <LinearLayout android:layout_width= "Fill_parent"Android:layout_height= "55SP"Android:layout_weight= "1"Android:background= "@color/burlywood"android:orientation= "Horizontal" > <TextView Android:id= "@+id/tvcalendar"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"Android:layout_weight= "1"Android:drawabletop= "@drawable/calendar"android:gravity= "Center"Android:text= "@string/calendar"android:textsize= "12SP"Tools:ignore= "Nestedweights" > </TextView> <TextView Android:id= "@+id/tvreminder"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"Android:layout_weight= "1"Android:drawabletop= "@drawable/reminder"android:gravity= "Center"Android:text= "@string/reminder"android:textsize= "12SP" > </TextView> <TextView Android:id= "@+id/tvnote"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"Android:layout_weight= "1"Android:drawabletop= "@drawable/note"android:gravity= "Center"Android:text= "@string/note"android:textsize= "12SP" > </TextView> <TextView Android:id= "@+id/tvmore"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"Android:layout_weight= "1"Android:drawabletop= "@drawable/more"android:gravity= "Center"Android:text= "@string/more"android:textsize= "12SP" > </TextView> </LinearLayout></LinearLayout>

Record the use of Fragmentactivity+viewpager+adapter for later use.

Use of Android Fragmentactivity+viewpager

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.