読者です 読者をやめる 読者になる 読者になる

【Android】ViewPagerとFragmentTabHost

 

f:id:stanleyblenz:20161113123033p:plain

 

---------------------------------------------------------------------------------------------------------

build.gradle
dependencies{
  compile 'com.android.support:support-v4:24.2.0'
}

---------------------------------------------------------------------------------------------------------

main_activity.xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TabWidget
      android:id="@android:id/tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
    <FrameLayout
      android:id="@android:id/tabcontent"
      android:layout_width="0dp"
      android:layout_height="0dp"/>
  </TabHost>
  <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

---------------------------------------------------------------------------------------------------------

MainActivity.java

public class MainActivity extends FragmentActivity
       implements TabHost.OnTabChangeListener,

       ViewPager.OnPageChangeListener {

  ViewPager viewPager = null;
  TabHost tabHost = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);//タイトル非表示
    setContentView(R.layout.main_activity);

    //Tabにページ部分の紐づけ
    PagerAdapter pa = new PagerAdapter(getSupportFragmentManager());
    tabHost = (TabHost)findViewById(android.R.id.tabhost);
    tabHost.setup();
    for (int i = 0; i < pa.getCount(); i++) {
      tabHost.addTab(tabHost
       .newTabSpec(String.valueOf(i))
       .setIndicator(new MyView(this, (String)pa.getPageTitle(i), i))
       .setContent(android.R.id.tabcontent));
    }
    tabHost.setOnTabChangedListener(this);//リスナー
    tabHost.getTabWidget().getChildAt(2).setVisibility(View.GONE);//3番目のタビ非表示

    //ViewPagerにページ部分の紐づけ
    viewPager = (ViewPager)findViewById(R.id.viewpager);
    viewPager.setAdapter(pa);
    viewPager.addOnPageChangeListener(this);//リスナー
  }
  @Override
  public void onTabChanged(String tabId) {
    //TabHost(Tabが切り替わった時に呼び出される)
    viewPager.setCurrentItem(Integer.valueOf(tabId));
  }
  @Override
  public void onPageSelected(int position) {
    //ViewPage(ページが切り替わった時に呼び出される)
    tabHost.setCurrentTab(position);
    if (position == 0) {
      tabHost.getTabWidget().getChildAt(0).setVisibility(View.VISIBLE);
      tabHost.getTabWidget().getChildAt(1).setVisibility(View.VISIBLE);
      tabHost.getTabWidget().getChildAt(2).setVisibility(View.GONE);
    } else if (position == 1) {
      tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
      tabHost.getTabWidget().getChildAt(1).setVisibility(View.VISIBLE);
      tabHost.getTabWidget().getChildAt(2).setVisibility(View.VISIBLE);
    } else if (position == 2) {
      tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
      tabHost.getTabWidget().getChildAt(1).setVisibility(View.VISIBLE);
      tabHost.getTabWidget().getChildAt(2).setVisibility(View.VISIBLE);
    }
  }
  @Override
  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
   //ViewPager(スクロール状態が変化したときに呼び出される)
  }
  @Override
  public void onPageScrollStateChanged(int state) {
    //ViewPager
  }
  // アダプタークラス FragmentStatePagerAdapter
  public class PagerAdapter extends FragmentStatePagerAdapter {
    public PagerAdapter(FragmentManager fm) {
      super(fm);
    }
    @Override
    public Fragment getItem(int position) {
      switch (position){
        case 0:
          return new Fragment1();
        case 1:
          return new Fragment2();
        default:
          return new Fragment3();
      }
    }
    @Override
    public int getCount() {
      return 3;
    }
    @Override
    public CharSequence getPageTitle(int position) {
      if (position == 0) {
        return "ページ1";
      } else if (position == 1) {
        return "ページ2";
      } else if (position == 2) {
        return "ページ3";
      }
      return "";
    }
  }
  private class MyView extends FrameLayout {
    private LayoutInflater inflater;
    public MyView(Context context) {
      super(context);
      inflater = LayoutInflater.from(context);
    }
    public MyView(Context context, String title, int position) {
      this(context);
      View v;
      TextView tv;
      if (position == 0) {
        v = inflater.inflate(R.layout.textview1, null);
        tv = (TextView)v.findViewById(R.id.textview1);
      } else if (position == 1) {
        v = inflater.inflate(R.layout.textview2, null);
        tv = (TextView)v.findViewById(R.id.textview2);
      } else {
        v = inflater.inflate(R.layout.textview3, null);
        tv = (TextView)v.findViewById(R.id.textview3);
      }
      tv.setText(title);
      addView(v);
    }
  }
}

---------------------------------------------------------------------------------------------------------

Fragment1.java

public class Fragment1 extends Fragment {
  public View onCreateView(LayoutInflater inflater,

              ViewGroup container,

              Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment1, container, false);
    wv = (WebView)v.findViewById(R.id.webView1);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient());
    wv.loadUrl("http://www.yahoo.co.jp");//Webページ表示
    return v;
  }
}

---------------------------------------------------------------------------------------------------------

fragment1.xml

<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</FrameLayout>

---------------------------------------------------------------------------------------------------------

textview1.xm

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/textview1"
  android:gravity="center"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:padding="10dp"
  android:textColor="#ffffff"
  android:background="#00f0f0"/>

 

 

以上です。