0

我正在尝试在 a 中显示一个片段,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>
4

1 回答 1

1

这样做很简单

在您的按钮 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();
于 2013-04-30T03:58:58.463 回答