Android Open Source - Module3-DreamPlanner Swipe Tabs






From Project

Back to project page Module3-DreamPlanner.

License

The source code is released under:

Apache License

If you think the Android project Module3-DreamPlanner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.pongdang.swipetabsdialog;
/*from w w w  .  j  a  v  a2  s  .  c  o m*/
import java.util.List;
import java.util.Vector;

import com.example.swipetabsdialog.R;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;

public class SwipeTabs extends FragmentActivity implements
    TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

  private TabHost mTabHost;
  
  private ViewPager mViewPager;
  private PagerAdapter mPagerAdapter;
  List<Fragment> fragments = new Vector<Fragment>();
  

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.swipetabs);
    
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();
    createTabnFragment("Tab1", Tab1.class, savedInstanceState);
    createTabnFragment("Tab2", Tab2.class, savedInstanceState);
    createTabnFragment("Tab3", Tab3.class, savedInstanceState);
    
    initViewPager();
    
    mTabHost.setCurrentTab(2);
  }
  
  private void createTabnFragment(String tabName, Class<?> cls, Bundle savedInstanceState) {
    initTab(tabName, cls, savedInstanceState); //Tab??? ???.
    fragments.add(Fragment.instantiate(this, cls.getName())); //ViewPager??? Fragment? ???.
  }

  private void initTab(String tabName, Class<?> cls, Bundle savedInstanceState) {
    TabSpec tabSpec = mTabHost.newTabSpec(cls.getSimpleName()).setIndicator(tabName);
    tabSpec.setContent(new TabFactory(this));
    mTabHost.addTab(tabSpec);
    mTabHost.setOnTabChangedListener(this);
  }
  
  private void initViewPager() {
    mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
    
    mViewPager = (ViewPager) findViewById(R.id.tabviewpager);
    mViewPager.setAdapter(mPagerAdapter);
    mViewPager.setOnPageChangeListener(this);
  }

  @Override
  public void onTabChanged(String tag) {
    mViewPager.setCurrentItem(mTabHost.getCurrentTab());
  }
  
  @Override
  public void onPageScrollStateChanged(int position) {
  }

  @Override
  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  }

  @Override
  public void onPageSelected(int position) {
    mTabHost.setCurrentTab(position);
  }

  class TabFactory implements TabContentFactory {

    private final Context mContext;

    public TabFactory(Context context) {
      mContext = context;
    }

    public View createTabContent(String tag) {
      View v = new View(mContext);
      v.setMinimumWidth(0);
      v.setMinimumHeight(0);
      return v;
    }
  }
  
  class MyPagerAdapter extends FragmentPagerAdapter {
    
    private List<Fragment> fragments;

    public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
      super(fm);
      this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
      return fragments.get(position);
    }

    @Override
    public int getCount() {
      return fragments.size();
    }
    
  }
}




Java Source Code List

com.github.pongdang.swipetabsdialog.MainActivity.java
com.github.pongdang.swipetabsdialog.SwipeTabs.java
com.github.pongdang.swipetabsdialog.Tab1.java
com.github.pongdang.swipetabsdialog.Tab2.java
com.github.pongdang.swipetabsdialog.Tab3.java