にフラグメントを表示しようとしViewPager
ていますが、フラグメントを置き換えるボタンが必要ViewPager
です。私はstackoverflowにあるexamplesIiを使ってセットアップすることができました。ただし、2 つの fragmentA と FragmentB を切り替えるにはどうすればよいですか。
どうもありがとう。
私のコードは次のとおりです。
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final FragmentManager mFragmentManager;
private Fragment mFragmentAtPos0;
private Fragment mFragmentAtPos1;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
mFragmentManager=fm;
}
@Override
public int getItemPosition(Object object)
{
if (object instanceof FragmentA && mFragmentAtPos0 instanceof FragmentB)
return POSITION_NONE;
if (object instanceof FragmentB && mFragmentAtPos1 instanceof FragmentA)
return POSITION_NONE;
return POSITION_UNCHANGED;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch (position){
case 1:
if (mFragmentAtPos1 == null)
{
mFragmentAtPos1 = FragmentB.newInstance(new FirstPageFragmentListener()
{
public void onSwitchToNextFragment()
{
mFragmentManager.beginTransaction().remove(mFragmentAtPos1).commit();
mFragmentAtPos1 = new FragmentA();
notifyDataSetChanged();
showToast("in switchtoNextFragmentA ------------1");
}
});
}
return mFragmentAtPos1;
case 0:
if (mFragmentAtPos0 == null)
{
mFragmentAtPos0 = FragmentA.newInstance(new FirstPageFragmentListener()
{
public void onSwitchToNextFragment()
{
mFragmentManager.beginTransaction().remove(mFragmentAtPos0).commit();
mFragmentAtPos0 = new FragmentB();
notifyDataSetChanged();
showToast("in switchtoNextFragment");
}
});
}
return mFragmentAtPos0;
default: return null;
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
public class FragmentB extends Fragment {
Button button;
static FragmentB b;
static FirstPageFragmentListener fpl1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
return inflater.inflate(R.layout.frag_b, group, false);
}
public static Fragment newInstance(FirstPageFragmentListener firstPageFragmentListener) {
// TODO Auto-generated method stub
fpl1 = firstPageFragmentListener;
if (b == null)
b = new FragmentB();
return b;
}
public static FragmentB newInstance() {
if (b == null)
b = new FragmentB();
return b;
}
@Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
button = (Button) getActivity().findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fpl1.onSwitchToNextFragment();
}
});
}
}
更新: レイアウト ファイル
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/lbltime"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:id="@+id/framelayout">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFCC" >
</android.support.v4.view.ViewPager>
</FrameLayout>