package com.exam.fragment;
import java.util.ArrayList;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends ActionBarActivity {
private ArrayList<Fragment> f_list = new ArrayList<Fragment>();
private ViewPager viewpager;
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
initviewPager();
initRadioGroup();
}
private void initRadioGroup() {
// TODO Auto-generated method stub
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.radio0:
viewpager.setCurrentItem(0, false);
break;
case R.id.radio1:
viewpager.setCurrentItem(1, false);
break;
case R.id.radio2:
viewpager.setCurrentItem(2, false);
break;
case R.id.radio3:
viewpager.setCurrentItem(3, false);
break;
}
}
});
}
private void initviewPager() {
// TODO Auto-generated method stub
f_list.add(new F1_class());
f_list.add(new F2_class());
f_list.add(new F3_class());
f_list.add(new F4_class());
viewpager.setAdapter(new MyViewpagerAdapter(
getSupportFragmentManager(), f_list));
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
int currentItem = viewpager.getCurrentItem();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
radioGroup.check(R.id.radio0);
break;
case 1:
radioGroup.check(R.id.radio1);
break;
case 2:
radioGroup.check(R.id.radio2);
break;
case 3:
radioGroup.check(R.id.radio3);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private void initView() {
viewpager = (ViewPager) findViewById(R.id.viewpager);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
}
}