碎片内显示的两个碎片(页面)

时间:2013-08-01 03:45:58

标签: java android android-fragments

我试图通过rightfrag在ViewPager的一个页面中显示两个片段'leftfrag'和MyFragmentPagerAdapter。在case 0中,在此特定AndroidFragment内实例化page新片段,我想显示两个片段leftfrag and rightfrag。我尝试创建另一个xml到inflate而不是 main 来包含这两个片段但是没有错误没有视图。我特别迷失在此。谢谢

MainActivity

public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
...
setContentView(R.layout.main); //Got confused with this xml
 MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm, this);
...
}

MyFragmentPagerAdapter

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
@Override
    public Fragment getItem(int arg0) {

        switch(arg0){
            /** Android tab is selected */
            case 0:
                AndroidFragment androidFragment = new AndroidFragment();
                androidFragment.AFragment(mContext);
                return androidFragment;
}

AndroidFragment

public class AndroidFragment extends SherlockFragment{

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

         View rootView = inflater.inflate(R.layout.main, container, false);
             //I am trying to replace or add two fragments in the main.xml see xml below

         final int MARGIN = 16;
        leftFrag = new RoundedColourFragment(getResources().getColor(
                R.color.android_green), 1f, MARGIN, MARGIN / 2, MARGIN, MARGIN);

        rightFrag = new RoundedColourFragment(getResources().getColor(
                R.color.honeycombish_blue), 2f, MARGIN / 2, MARGIN, MARGIN,
                MARGIN);

         //Should I use Replace?
         //ft.replace(R.id.fragg, leftFrag, "left");
         //ft.replace(R.id.fragg2, rightFrag, "right");

        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
         ft.add(R.id.fragg, leftFrag, "left");
         ft.add(R.id.fragg2, rightFrag, "right");
         ft.addToBackStack(null);
         ft.commit();
}

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

     <FrameLayout 

            android:id="@+id/fragg"
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

      <FrameLayout

            android:id="@+id/fragg2"
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

</LinearLayout>

RoundedColourFragment

public class RoundedColourFragment extends SherlockFragment {

    private View mView;
    private int mColour;
    private float mWeight;
    private int marginLeft, marginRight, marginTop, marginBottom;

    // need a public empty constructor for framework to instantiate
    public RoundedColourFragment() {

    }

    public RoundedColourFragment(int colour, float weight, int margin_left,
            int margin_right, int margin_top, int margin_bottom) {
        mColour = colour;
        mWeight = weight;
        marginLeft = margin_left;
        marginRight = margin_right;
        marginTop = margin_top;
        marginBottom = margin_bottom;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = new View(getActivity());

        GradientDrawable background = (GradientDrawable) getResources()
                .getDrawable(R.drawable.rounded_rect);
        background.setColor(mColour);

        mView.setBackgroundDrawable(background);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
                LayoutParams.FILL_PARENT, mWeight);
        lp.setMargins(marginLeft, marginTop, marginRight, marginBottom);
        mView.setLayoutParams(lp);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return mView;
    }

}

0 个答案:

没有答案