public class MainActivity extends FragmentActivity {
private ViewPager vp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragment=null;
switch (arg0) {
case 0:
fragment=new Fragment1();
break;
case 1:
fragment=new Fragment2();
break;
default:
break;
}
return fragment;
}
});
}
//点击切换
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tv_one:
vp.setCurrentItem(0);
break;
case R.id.tv_tow:
vp.setCurrentItem(1);
break;
default:
break;
}
}
-------------
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.layou_fragment1, null);
return view;
}
}