我正在尝试在ViewPager
中显示一个片段,我想有一个按钮来替换ViewPager
中的片段。我设法使用在stackoverflow上找到的examplesIi进行设置。但是,如何在两个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();
}
});
}
}
UPDATE:布局文件
<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>
最佳答案
简单就做到这一点
在按钮的onClick方法中
Fragment f;
f = new FragmentTwo();
FragmentTransaction ft = getFragmentSupportManager().beginTransaction();
ft.replace(R.id.main_details_fragment_container, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();