import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
private RadioGroup radioGroup;
private List<Fragment> list;
private TextView text1,text2,text3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源ID
viewPager=(ViewPager) findViewById(R.id.viewpager);
radioGroup=(RadioGroup) findViewById(R.id.radio);
text1=(TextView) findViewById(R.id.text1);
text2=(TextView) findViewById(R.id.text2);
text3=(TextView) findViewById(R.id.text3);
text1.setVisibility(View.VISIBLE);
text2.setVisibility(View.INVISIBLE);
text3.setVisibility(View.INVISIBLE);
//将Fragment添加到集合
list=new ArrayList<Fragment>();
list.add(new Fragment01());
list.add(new Fragment02());
list.add(new Fragment03());
//设置FragmentPagerAdapter适配器将fragment添加
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
//当前fragmnet的数量
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
//加载Fragment
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
});
//对Viewpager添加添加 选中那一页就选中那个button
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
//判断选中的页数来选中button 来改变当前游标的颜色
switch (arg0) {
case 0:
radioGroup.check(R.id.but01);
//控制当前控件的显隐状态
//显示
text1.setVisibility(View.VISIBLE);
text2.setVisibility(View.INVISIBLE);
text3.setVisibility(View.INVISIBLE);
break;
case 1:
radioGroup.check(R.id.but02);
text1.setVisibility(View.INVISIBLE);
text2.setVisibility(View.VISIBLE);
text3.setVisibility(View.INVISIBLE);
break;
case 2:
radioGroup.check(R.id.but03);
text1.setVisibility(View.INVISIBLE);
text2.setVisibility(View.INVISIBLE);
text3.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
//对radiogroup添加监听 进行检查下面的button的id 进行选中当前的页
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.but01:
viewPager.setCurrentItem(0);
break;
case R.id.but02:
viewPager.setCurrentItem(1);
break;
case R.id.but03:
viewPager.setCurrentItem(2);
break;
default:
break;
}
}
});
}
}