Viewpager Slide button tab to change color

Source: Internet
Author: User

Package Com.example.day8viewpager;

Import java.util.ArrayList;
Import java.util.List;

Import Android.os.Bundle;

Import android.app.Activity;
Import Android.graphics.Color;
Import Android.support.v4.view.ViewPager;
Import Android.support.v4.view.ViewPager.OnPageChangeListener;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Mainactivity extends Activity implements Onclicklistener {

Private Button button1;
Private Button button2;
Private Button Button3;
Private Viewpager Viewpager;
Private list<view> List;

@Override
protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
//Set false data, Viewpager each item to display
SetData ();
button1 = (Button) Findviewbyid (r.id.button1);
Button2 = ( Button) Findviewbyid (R.id.button2);
Button3 = (Button) Findviewbyid (R.id.button3);
Button1.setonclicklistener (this);
Button2.setonclicklistener (this);
Button3.setonclicklistener (this);
Viewpager = (viewpager) Findviewbyid (R.id.viewpager);
//set adapter;
Viewpager.setadapter (new Mypageradapter (list));
//Set Viewpager default view shown when first come in;
Viewpager.setcurrentitem (0);
//First load the text color of the button corresponding to the Viewpager page
Setbuttoncolor (Viewpager.getcurrentitem ());

//viewpager page Change listener
Viewpager.setonpagechangelistener (new Onpagechangelistener () {
//viewpager view currently displayed
@Override
public void onpageselected (int arg0) {
//TODO auto-generated method Stub
Setbuttoncolor (ARG0);
}
//viewpager the sliding distance, and the displayed view; parameter one: The view to be displayed, parameter two: The offset of the slide; parameter three: sliding pixels
@Override
public void onpagescrolled (int arg0, float arg1, int arg2) {
//TODO auto-generated method stub

}
//Current page Change status
//stop status Scroll_state_idle 0
//drag state Scroll_state_ Dragging 1
//fixed state scroll_state_settling 2
@Override
public void onpagescrollstatechanged (int arg0) {
// TODO auto-generated method Stub

}
});
}
/**
* Sets the color of the button
* @param i viewpager the position of the current page
*/
private void Setbuttoncolor (int. Item) {
//TODO auto-generated Method stub
Switch (CurrentItem) {
Case 0:
Button1.settextcolor (color.white );
Button2.settextcolor (Color.Black);
Button3.settextcolor (Color.Black);
Break;

Case 1:
Button2.settextcolor (Color.White);
Button1.settextcolor (Color.Black);
Button3.settextcolor (Color.Black);
Break
Case 2:
Button3.settextcolor (Color.White);
Button1.settextcolor (Color.Black);
Button2.settextcolor (Color.Black);
Break

}
}
/**
* Set false data, Viewpager each item to display the content
*/
private void SetData () {
List = new arraylist<view> ();
View view1=view.inflate (mainactivity.this, r.layout.one, NULL);
View view2=view.inflate (mainactivity.this, r.layout.two, NULL);
View view3=view.inflate (mainactivity.this, r.layout.three, NULL);
List.add (View1);
List.add (VIEW2);
List.add (VIEW3);
}

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.id.button1:
Viewpager.setcurrentitem (0);
Break

Case R.id.button2:
Viewpager.setcurrentitem (1);
Break
Case R.ID.BUTTON3:
Viewpager.setcurrentitem (2);
Break

}
}



}

Viewpager Slide button tab to change color

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.