- public class ViewPagerActivity extends Activity {
-
- List<View> listViews;
-
- Context context = null;
-
- LocalActivityManager manager = null;
-
- TabHost tabHost = null;
-
- private ViewPager pager = null;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.viewpager);
-
- context = ViewPagerActivity.this;
-
- pager = (ViewPager) findViewById(R.id.viewpager);
-
-
- listViews = new ArrayList<View>();
-
- manager = new LocalActivityManager(this, true);
- manager.dispatchCreate(savedInstanceState);
-
- Intent i1 = new Intent(context, T1Activity.class);
- listViews.add(getView("A", i1));
- Intent i2 = new Intent(context, T2Activity.class);
- listViews.add(getView("B", i2));
- Intent i3 = new Intent(context, T3Activity.class);
- listViews.add(getView("C", i3));
-
- tabHost = (TabHost) findViewById(R.id.tabhost);
- tabHost.setup();
- tabHost.setup(manager);
-
-
-
- RelativeLayout tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tabwidget, null);
- TextView tvTab1 = (TextView)tabIndicator1.findViewById(R.id.tv_title);
- tvTab1.setText("第一页");
-
- RelativeLayout tabIndicator2 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tabwidget,null);
- TextView tvTab2 = (TextView)tabIndicator2.findViewById(R.id.tv_title);
- tvTab2.setText("第二页");
-
- RelativeLayout tabIndicator3 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tabwidget,null);
- TextView tvTab3 = (TextView)tabIndicator3.findViewById(R.id.tv_title);
- tvTab3.setText("第三页");
-
- Intent intent = new Intent(context,EmptyActivity.class);
-
- tabHost.addTab(tabHost.newTabSpec("A").setIndicator(tabIndicator1).setContent(intent));
- tabHost.addTab(tabHost.newTabSpec("B").setIndicator(tabIndicator2).setContent(intent));
- tabHost.addTab(tabHost.newTabSpec("C").setIndicator(tabIndicator3).setContent(intent));
-
-
- pager .setAdapter(new MyPageAdapter(listViews));
- pager .setOnPageChangeListener(new OnPageChangeListener() {
- @Override
- public void onPageSelected(int position) {
-
- tabHost.setCurrentTab(position);
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- });
-
-
-
- tabHost.setOnTabChangedListener(new OnTabChangeListener() {
- @Override
- public void onTabChanged(String tabId) {
-
- if ("A".equals(tabId)) {
- pager.setCurrentItem(0);
- }
- if ("B".equals(tabId)) {
-
- pager.setCurrentItem(1);
- }
- if ("C".equals(tabId)) {
- pager.setCurrentItem(2);
- }
- }
- });
-
-
-
- }
-
- private View getView(String id, Intent intent) {
- return manager.startActivity(id, intent).getDecorView();
- }
-
- private class MyPageAdapter extends PagerAdapter {
-
- private List<View> list;
-
- private MyPageAdapter(List<View> list) {
- this.list = list;
- }
-
- @Override
- public void destroyItem(View view, int position, Object arg2) {
- ViewPager pViewPager = ((ViewPager) view);
- pViewPager.removeView(list.get(position));
- }
-
- @Override
- public void finishUpdate(View arg0) {
- }
-
- @Override
- public int getCount() {
- return list.size();
- }
-
- @Override
- public Object instantiateItem(View view, int position) {
- ViewPager pViewPager = ((ViewPager) view);
- pViewPager.addView(list.get(position));
- return list.get(position);
- }
-
- @Override
- public boolean isViewFromObject(View arg0, Object arg1) {
- return arg0 == arg1;
- }
-
- @Override
- public void restoreState(Parcelable arg0, ClassLoader arg1) {
- }
-
- @Override
- public Parcelable saveState() {
- return null;
- }
-
- @Override
- public void startUpdate(View arg0) {
- }
-
- }
-
- }
posted @
2015-12-08 17:58
请叫我码农怪蜀黍
阅读(
259)
评论()
编辑
收藏
举报