fragment
public class Open4Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflateAndSetupView(inflater, container, savedInstanceState, R.layout.aa_me_open4);
}
private View inflateAndSetupView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState, int layoutResourceId) {
View layout = inflater.inflate(layoutResourceId, container, false);
return layout;
}
}
适配器
public class OpenAdapter extends FragmentStatePagerAdapter {
private Class[] fragments;
public OpenAdapter(FragmentManager fm){
super(fm);
fragments = new Class[]{Open1Fragment.class, Open2Fragment.class, Open3Fragment.class,Open4Fragment.class};
}
@Override
public int getCount() {
return fragments.length;
}
@Override
public Fragment getItem(int position) {
try {
return (Fragment) fragments[position].newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
主界面
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OpenAdapter adapter=new OpenAdapter(getSupportFragmentManager());
ViewPager pager= (ViewPager) findViewById(R.id.vpager_open);
pager.setAdapter(adapter);
}
}