Android開発でViewPager利用時にFragmentでタブ位置を取得したいです。
Fragmentで呼び出されるJavaclassでタブ位置によって表示する内容を少し変更したいためです。
すみませんが、わからないのでご教授ください。
MainActivityは、ほぼAndroid Studioで作成される標準のまま利用しています。
tab1Activity
tab2Activity
の2つを呼び出していますが、この中でタブ位置により処理を変更したいです
以下の方法でいろいろ試していますがうまくいっていません。
(1)FragmentのActivity(tab1Activity,tab2Activity)呼び出し時にタブ位置の引数を渡す
(2)FragmentのActivity(tab1Activity,tab2Activity)内で現在のタブ位置を取得する
public class MainActivity extends AppCompatActivity { private SectionsPagerAdapter mSectionsPagerAdapter; private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.AppTheme); setContentView(R.layout.activity_main); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(mViewPager); //アイコンセット tabLayout.getTabAt(0).setIcon(R.drawable.tab_iconsel_fav1); tabLayout.getTabAt(1).setIcon(R.drawable.tab_iconsel_fav2); tabLayout.getTabAt(2).setIcon(R.drawable.tab_iconsel_tv); tabLayout.getTabAt(3).setIcon(R.drawable.tab_iconsel_air); tabLayout.getTabAt(4).setIcon(R.drawable.tab_iconsel_dvd); tabLayout.getTabAt(5).setIcon(R.drawable.tab_iconsel_lig); tabLayout.getTabAt(6).setIcon(R.drawable.menuset); } // deleted PlaceholderFragment from here /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: tab1Activity npanel0 = new tab1Activity(); return npanel0; case 1: tab2Activity npanel1 = new tab2Activity(); return npanel1; case 2: tab1Activity npanel2 = new tab1Activity(); return npanel2; case 3: tab2Activity npanel3 = new tab2Activity(); return npanel3; case 4: tab1Activity npanel4 = new tab1Activity(); return npanel4; case 5: tab2Activity npanel5 = new tab2Activity(); return npanel5; case 6: tab7MenuActivity npanel6 = new tab7MenuActivity(); return npanel6; default: return null; } } @Override public int getCount() { // Show 3 total pages. return 7; } @Override public CharSequence getPageTitle(int position) { return null; } } } public class tab1Activity extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.frag_tab1, container, false); //TextView textView = (TextView) rootView.findViewById(R.id.section_label); //textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); // ここからタブ位置による異なる処理を行いたい return rootView; } }
回答2件
あなたの回答
tips
プレビュー