Viewpager (bottom) -- use fragment to implement beautiful page Switching

Source: Internet
Author: User
Tags getcolor

The previously used viewpager is suitable for simple ad switching, but it is best to use the officially recommended fragment for page switching.

I strive to be the simplest and most practical. I want to use it later and copy it easily.

:



Function: you can click the button to switch to the page, or move the page to switch to the page.

Files: 5 java files, 4 XML files, and 4. 9 images

Java main class:

<Span style = "font-size: 12px;"> package COM. example. viewpageractivity; import android. OS. bundle; import android. support. v4.app. fragmentactivity; import android. support. v4.view. viewpager; import android. support. v4.view. viewpager. onpagechangelistener; import android. view. view; import android. widget. button; public class mainactivity extends fragmentactivity {public viewpager mviewpager; Public button bt_myclass_studing; Public button bt_myclass_over; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); // call the encapsulated actionbar buildingactionbar. setactionbarlayout (R. layout. actionbar, mainactivity. this); bt_myclass_studing = (button) This. findviewbyid (R. id. id_myclass_button_studing); bt_myclass_over = (button) This. findviewbyid (R. id. id_myclass_button_over); bt_myclass_studing.settext ("in progress"); bt_myclass_over.settext ("completed"); listener (New mybuttonlistener ()); // build the adapter fragmenadapter mfragmenadapter = new fragmenadapter (this. getsuppfrfragmentmanager (); mviewpager = (viewpager) This. findviewbyid (R. id. id_myclass_viewpager); mviewpager. setadapter (mfragmenadapter); // Add a sliding listener mviewpager. setonpagechangelistener (New onpagechangelistener () {@ overridepublic void onpagescrollstatechanged (INT arg0) {}@ overridepublic void onpagescrolled (INT arg0, float arg1, int arg2) {}@ overridepublic void onpageselected (INT arg0) {Switch (arg0) {Case 0: setbuttoncolor (true); break; Case 1: setbuttoncolor (false ); break ;}}); setbuttoncolor (true);}/*** set the button background color * attaches the color value * liteblue: # a1dff8 **/Public void setbuttoncolor (Boolean mytouchbutton) {If (mytouchbutton = true) {// click bt_myclass_studing.setbackgroundresource (R. drawable. title_switch_left_on); bt_myclass_studing.settextcolor (getresources (). getcolor (R. color. white); bt_myclass_over.setbackgroundresource (R. drawable. title_switch_right_off); bt_myclass_over.settextcolor (getresources (). getcolor (R. color. liteblue); mviewpager. setcurrentitem (0, true);} else {// click bt_myclass_over.setbackgroundresource (R. drawable. title_switch_right_on); bt_myclass_over.settextcolor (getresources (). getcolor (R. color. white); bt_myclass_studing.setbackgroundresource (R. drawable. title_switch_left_off); bt_myclass_studing.settextcolor (getresources (). getcolor (R. color. liteblue); mviewpager. setcurrentitem (1, true) ;}/ *** button listener **/class mybuttonlistener implements view. onclicklistener {@ overridepublic void onclick (view arg0) {Switch (arg0.getid () {case R. id. id_myclass_button_studing: setbuttoncolor (true); break; case R. id. id_myclass_button_over: setbuttoncolor (false); break ;}}</span>

Fragmentpageradapter adapter class

<span style="font-size:12px;">package com.example.viewpageractivity;import java.util.ArrayList;import java.util.List;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;public class FragmenAdapter extends FragmentPagerAdapter{List<Fragment> mFragment;public FragmenAdapter(FragmentManager fm) {super(fm);mFragment = new ArrayList<Fragment>();mFragment.add(new FragmentActivityA());mFragment.add(new FragmentActivityB());}@Overridepublic Fragment getItem(int arg0) {return mFragment.get(arg0);}@Overridepublic int getCount() {return mFragment.size();}}</span>

Fragmentactivitya class

<Span style = "font-size: 12px;"> package COM. example. viewpageractivity; import android. OS. bundle; import android. support. v4.app. fragment; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; public class fragmentactivitya extends fragment {public view myview; @ overridepublic view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {// Add layout file myview = Inflater. inflate (R. layout. fragmenta, container, false); Return myview ;}</span>


Fragmentactivityb class

<Span style = "font-size: 12px;"> package COM. example. viewpageractivity; import android. OS. bundle; import android. support. v4.app. fragment; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; public class fragmentactivityb extends fragment {public view myview; @ overridepublic view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {// Add layout file myview = Inflater. inflate (R. layout. fragmentb, container, false); Return myview ;}</span>

Previously Used encapsulated actionbar class

<Span style = "font-size: 12px;"> package COM. example. viewpageractivity; import android. app. actionbar; import android. app. actionbar. layoutparams; import android. app. activity; import android. content. context; import android. util. log; import android. view. layoutinflater; import android. view. view;/*** set action bar **/public class buildingactionbar {public static actionbar mactionbar;/*** set action bar **/Public Static void setactionbarlayout (INT layoutid, activity mactivity) {mactionbar = mactivity. getactionbar (); If (mactionbar! = NULL) {// set the home title to hide the mactionbar. setdisplayshowhomeenabled (false); // enables custom normal views to be displayed in the title bar, that is, actionbar. setcustomview can work on mactionbar. setdisplayshowcustomenabled (true); layoutinflater inflator = (layoutinflater) mactivity. getsystemservice (context. layout_inflater_service); view v = inflator. inflate (layoutid, null); actionbar. layoutparams layout = new actionbar. layoutparams (layoutparams. match_parent, layoutparams. match_parent); mactionbar. setcustomview (v, layout);} else {log. E ("buildingactionbar", "actionbar is blank") ;}}</span>

XML file of actionbar

<Span style = "font-size: 12px;"> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ + ID/actionbarlayoutid" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Background = "#1d92f9"> <textview Android: Id = "@ + ID/actionbar_left" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_centervertical = "true" Android: textcolor = "# fff" Android: layout_marginleft = "3dip" Android: TEXT = "Left" Android: textsize = "20sp"/> <relativelayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_centerinparent = "true" Android: layout_centervertical = "true"> <button Android: Id = "@ + ID/id_myclass_button_studing" Android: layout_width = "wrap_content" Android: layout_height = "48dip" Android: background = "@ drawable/title_switch_left_off" Android: textcolor = "# a3a3a3" Android: textsize = "15sp"/> <button Android: Id = "@ + ID/id_myclass_button_over" Android: layout_width = "wrap_content" Android: layout_height = "48dip" Android: layout_torightof = "@ ID/id_myclass_button_studing" Android: Background = "@ drawable/strong" Android: textcolor = "# a3a3a3" Android: textsize = "15sp"/> </relativelayout> </span>


XML file of the main class

<span style="font-size:12px;"><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"    tools:context=".MainActivity" >    <android.support.v4.view.ViewPager        android:id="@+id/id_myclass_viewpager"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout></span>


There are also two layout files for fragmentactivity that can be customized and no longer attached.


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.