1

I am trying to display the two fragments 'leftfrag' and rightfrag, in one of the ViewPager's pages through MyFragmentPagerAdapter. In case 0 a new fragment is instantiated AndroidFragment inside this particular page, I'd like to display the two fragments leftfrag and rightfrag. I tried creating another xml to inflate Not the main to contain the two fragments but got errors no views. I am particularity lost at this. Thanks

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;
    }

}
rahstame
  • 2,148
  • 4
  • 23
  • 53
  • 1
    Please stop adding `Android`(or other tags) to your question's title, the tag at the bottom is enough. If you want two nested fragments in the Androidfragment then inflate a layout for it that has two placeholders and add the fragments there through a transaction. – user Aug 01 '13 at 04:32
  • Copy sir. I tried using another layout aside from `main`, http://pastie.org/8195810 but still the fragments are not displayed. – rahstame Aug 01 '13 at 05:13
  • 1
    Use this layout file for the AndroidFragment and see how it goes https://gist.github.com/luksprog/6128982 . Why do you you assume that the parent of the view returned from the RoundedColourFragment fragment will be a LinearLayout? – user Aug 01 '13 at 06:48
  • you should search in the Internet with this title "android drawerlayout" There are some good answer: [http://stackoverflow.com/q/16954679/967548](http://stackoverflow.com/q/16954679/967548) [http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html](http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html) – Omid Nazifi Aug 01 '13 at 05:25
  • After a series of trial and error, I found out that `ft.add(R.id.root, leftFrag, "left");`, `ft.add(R.id.root, rightFrag, "right");` resolves it. From this aspect, the layout it self is the one used by the `leftFrag` and `rightFrag` fragments to put its views, and I just deleted the `FrameLayout`'s. I did not realize how `RoundedColourFragment` made this happen. Do you have an idea on this @Luksprog? – rahstame Aug 02 '13 at 03:49

0 Answers0