質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

1回答

2479閲覧

TabbedActivityの用い方について

sukuraapplideve

総合スコア11

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2015/09/06 14:12

編集2022/01/12 10:55

AndroidStudioの新規プロジェクト作成からTabbedActivityを選び、NavigationStyleをActionBarTabs(with ViewPager)としました。
私はこのプロジェクトでは3つのフラグメントをタブとフリックで切り替えでき、それぞれのフラグメントを別々にレイアウトされたアプリを作りたいです。
初期設定では、タブの数は3つとなっておりそのままでよいのです。しかし、その3つのタブに対応させてフラグメントを設置させる方法がわかりません。
どの部分を変更させればタブに対するフラグメントを設置できるのかを教えてください。
新規作成時は下のようなコードとなっています。

java

1package com.example.sample.myapplication; 2 3import java.util.Locale; 4 5import android.support.v7.app.AppCompatActivity; 6import android.support.v7.app.ActionBar; 7import android.support.v4.app.Fragment; 8import android.support.v4.app.FragmentManager; 9import android.support.v4.app.FragmentTransaction; 10import android.support.v4.app.FragmentPagerAdapter; 11import android.os.Bundle; 12import android.support.v4.view.ViewPager; 13import android.view.Gravity; 14import android.view.LayoutInflater; 15import android.view.Menu; 16import android.view.MenuItem; 17import android.view.View; 18import android.view.ViewGroup; 19import android.widget.TextView; 20 21public class MainActivity extends AppCompatActivity implements ActionBar.TabListener { 22 23 /** 24 * The {@link android.support.v4.view.PagerAdapter} that will provide 25 * fragments for each of the sections. We use a 26 * {@link FragmentPagerAdapter} derivative, which will keep every 27 * loaded fragment in memory. If this becomes too memory intensive, it 28 * may be best to switch to a 29 * {@link android.support.v4.app.FragmentStatePagerAdapter}. 30 */ 31 SectionsPagerAdapter mSectionsPagerAdapter; 32 33 /** 34 * The {@link ViewPager} that will host the section contents. 35 */ 36 ViewPager mViewPager; 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_main); 42 43 // Set up the action bar. 44 final ActionBar actionBar = getSupportActionBar(); 45 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 46 47 // Create the adapter that will return a fragment for each of the three 48 // primary sections of the activity. 49 mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 50 51 // Set up the ViewPager with the sections adapter. 52 mViewPager = (ViewPager) findViewById(R.id.pager); 53 mViewPager.setAdapter(mSectionsPagerAdapter); 54 55 // When swiping between different sections, select the corresponding 56 // tab. We can also use ActionBar.Tab#select() to do this if we have 57 // a reference to the Tab. 58 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 59 @Override 60 public void onPageSelected(int position) { 61 actionBar.setSelectedNavigationItem(position); 62 } 63 }); 64 65 // For each of the sections in the app, add a tab to the action bar. 66 for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 67 // Create a tab with text corresponding to the page title defined by 68 // the adapter. Also specify this Activity object, which implements 69 // the TabListener interface, as the callback (listener) for when 70 // this tab is selected. 71 actionBar.addTab( 72 actionBar.newTab() 73 .setText(mSectionsPagerAdapter.getPageTitle(i)) 74 .setTabListener(this)); 75 } 76 } 77 78 79 @Override 80 public boolean onCreateOptionsMenu(Menu menu) { 81 // Inflate the menu; this adds items to the action bar if it is present. 82 getMenuInflater().inflate(R.menu.menu_main, menu); 83 return true; 84 } 85 86 @Override 87 public boolean onOptionsItemSelected(MenuItem item) { 88 // Handle action bar item clicks here. The action bar will 89 // automatically handle clicks on the Home/Up button, so long 90 // as you specify a parent activity in AndroidManifest.xml. 91 int id = item.getItemId(); 92 93 //noinspection SimplifiableIfStatement 94 if (id == R.id.action_settings) { 95 return true; 96 } 97 98 return super.onOptionsItemSelected(item); 99 } 100 101 @Override 102 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 103 // When the given tab is selected, switch to the corresponding page in 104 // the ViewPager. 105 mViewPager.setCurrentItem(tab.getPosition()); 106 } 107 108 @Override 109 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 110 } 111 112 @Override 113 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 114 } 115 116 /** 117 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 118 * one of the sections/tabs/pages. 119 */ 120 public class SectionsPagerAdapter extends FragmentPagerAdapter { 121 122 public SectionsPagerAdapter(FragmentManager fm) { 123 super(fm); 124 } 125 126 @Override 127 public Fragment getItem(int position) { 128 // getItem is called to instantiate the fragment for the given page. 129 // Return a PlaceholderFragment (defined as a static inner class below). 130 return PlaceholderFragment.newInstance(position + 1); 131 } 132 133 @Override 134 public int getCount() { 135 // Show 3 total pages. 136 return 3; 137 } 138 139 @Override 140 public CharSequence getPageTitle(int position) { 141 Locale l = Locale.getDefault(); 142 switch (position) { 143 case 0: 144 return getString(R.string.title_section1).toUpperCase(l); 145 case 1: 146 return getString(R.string.title_section2).toUpperCase(l); 147 case 2: 148 return getString(R.string.title_section3).toUpperCase(l); 149 } 150 return null; 151 } 152 } 153 154 /** 155 * A placeholder fragment containing a simple view. 156 */ 157 public static class PlaceholderFragment extends Fragment { 158 /** 159 * The fragment argument representing the section number for this 160 * fragment. 161 */ 162 private static final String ARG_SECTION_NUMBER = "section_number"; 163 164 /** 165 * Returns a new instance of this fragment for the given section 166 * number. 167 */ 168 public static PlaceholderFragment newInstance(int sectionNumber) { 169 PlaceholderFragment fragment = new PlaceholderFragment(); 170 Bundle args = new Bundle(); 171 args.putInt(ARG_SECTION_NUMBER, sectionNumber); 172 fragment.setArguments(args); 173 return fragment; 174 } 175 176 public PlaceholderFragment() { 177 } 178 179 @Override 180 public View onCreateView(LayoutInflater inflater, ViewGroup container, 181 Bundle savedInstanceState) { 182 View rootView = inflater.inflate(R.layout.fragment_main, container, false); 183 return rootView; 184 } 185 } 186 187} 188

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

SessionPagerAdapterのgetItemの部分です。

PlaceHolderFragment.newInstance(position + 1)

ってのをreturnしてますよね?
つまりこれは、PlaceHolderFragmentにposition + 1という引数を食わせてインスタンス化したものを返しているわけせす。
positionってのは、タブを左から数えたときの番号です。
ちなみにこの場合、0から数え始めます。

Fragmentをインスタンス化する際に、position + 1という、タブの位置ごとに異なる引数を与えることで、各タブに合ったFragmentを返すことができるのです。
なので、PlaceHolderFragmentを作るときも、それに合わせて異なる内容を表示するようにしてやればいいのです。

投稿2015/11/02 22:12

mickamy9

総合スコア27

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問