package com.wzm.viewpager;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class MyFragmentActivity extends FragmentActivity {
private ViewPager pager;
private List<Fragment> list;
private List<TextView> tvs;
private List<View> vs;
private TextView tv1,tv2,tv3;
private View v1,v2,v3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_myfragment);
initTextView();
initView();
initData();
initpager();
}
private void initView() {
v1=(View) findViewById(R.id.v1);
v2=(View) findViewById(R.id.v2);
v3=(View) findViewById(R.id.v3);
v1.setBackgroundColor(Color.RED);
}
private void initTextView() {
tv1=(TextView) findViewById(R.id.tv1);
tv2=(TextView) findViewById(R.id.tv2);
tv3=(TextView) findViewById(R.id.tv3);
tv1.setTextColor(Color.RED);
tv1.setOnClickListener(new MyOnClickListener(0));
tv2.setOnClickListener(new MyOnClickListener(1));
tv3.setOnClickListener(new MyOnClickListener(2));
}
private class MyOnClickListener implements OnClickListener{
private int index;
public MyOnClickListener(int index){
this.index=index;
}
@Override
public void onClick(View v) {
//设置切换页面
pager.setCurrentItem(index);
vs.get(index).setBackgroundColor(Color.RED);
}
}
private void initData() {
list=new ArrayList<Fragment>();
list.add(new FirstFragment());
list.add(new SecondFragment());
list.add(new ThirdFragment());
tvs=new ArrayList<TextView>();
tvs.add(tv1);
tvs.add(tv2);
tvs.add(tv3);
vs=new ArrayList<View>();
vs.add(v1);
vs.add(v2);
vs.add(v3);
}
private void initpager() {
pager=(ViewPager) findViewById(R.id.pager);
//获取片段管理器对象
pager.setAdapter(new MyAdapter(getSupportFragmentManager()));
pager.setOnPageChangeListener(new MyOnPageChangeListener());
}
private class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}
//pager监听器
private class MyOnPageChangeListener implements OnPageChangeListener{
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
for(int i=0;i<tvs.size();i++){
if(i==arg0){
tvs.get(i).setTextColor(Color.RED);
vs.get(i).setBackgroundColor(Color.RED);
}else{
tvs.get(i).setTextColor(Color.BLUE);
vs.get(i).setBackgroundColor(0xffd2d422);
}
}
}
}
}
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class MyFragmentActivity extends FragmentActivity {
private ViewPager pager;
private List<Fragment> list;
private List<TextView> tvs;
private List<View> vs;
private TextView tv1,tv2,tv3;
private View v1,v2,v3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_myfragment);
initTextView();
initView();
initData();
initpager();
}
private void initView() {
v1=(View) findViewById(R.id.v1);
v2=(View) findViewById(R.id.v2);
v3=(View) findViewById(R.id.v3);
v1.setBackgroundColor(Color.RED);
}
private void initTextView() {
tv1=(TextView) findViewById(R.id.tv1);
tv2=(TextView) findViewById(R.id.tv2);
tv3=(TextView) findViewById(R.id.tv3);
tv1.setTextColor(Color.RED);
tv1.setOnClickListener(new MyOnClickListener(0));
tv2.setOnClickListener(new MyOnClickListener(1));
tv3.setOnClickListener(new MyOnClickListener(2));
}
private class MyOnClickListener implements OnClickListener{
private int index;
public MyOnClickListener(int index){
this.index=index;
}
@Override
public void onClick(View v) {
//设置切换页面
pager.setCurrentItem(index);
vs.get(index).setBackgroundColor(Color.RED);
}
}
private void initData() {
list=new ArrayList<Fragment>();
list.add(new FirstFragment());
list.add(new SecondFragment());
list.add(new ThirdFragment());
tvs=new ArrayList<TextView>();
tvs.add(tv1);
tvs.add(tv2);
tvs.add(tv3);
vs=new ArrayList<View>();
vs.add(v1);
vs.add(v2);
vs.add(v3);
}
private void initpager() {
pager=(ViewPager) findViewById(R.id.pager);
//获取片段管理器对象
pager.setAdapter(new MyAdapter(getSupportFragmentManager()));
pager.setOnPageChangeListener(new MyOnPageChangeListener());
}
private class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}
//pager监听器
private class MyOnPageChangeListener implements OnPageChangeListener{
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
for(int i=0;i<tvs.size();i++){
if(i==arg0){
tvs.get(i).setTextColor(Color.RED);
vs.get(i).setBackgroundColor(Color.RED);
}else{
tvs.get(i).setTextColor(Color.BLUE);
vs.get(i).setBackgroundColor(0xffd2d422);
}
}
}
}
}