1

The app for tablet for each screen into four parts(four fragment). I have added four fragment to fragments transaction in a single layout. Now I have to implement view pager in that. How can I achieve that. Using that view pager library I have to pass the fragment manager and fragmentlist as argument. In my scenario How can I pass every 4 argument in a single time?

My Main Activity was:

> public void onCreate(Bundle savedInstanceState) {
>       super.onCreate(savedInstanceState);
>       setContentView(R.layout.activity_main);         NewsFragment[]
> newsFragment_obj = new NewsFragment[GlobalValues.titile.length];
> 
>       fragMentTra = getFragmentManager().beginTransaction();
> 
>       for (int i = 0; i < GlobalValues.titile.length; i++) {
>           newsFragment_obj[i] = new NewsFragment(GlobalValues.titile[i],
>                   GlobalValues.content[i]);       }
> 
>       fragMentTra.add(R.id.fragment_container1, newsFragment_obj[0],
>               "Fragment1");       fragMentTra.add(R.id.fragment_container2, newsFragment_obj[1],
>               "Fragment2");       fragMentTra.add(R.id.fragment_container3, newsFragment_obj[2],
>               "Fragment3");       fragMentTra.add(R.id.fragment_container4, newsFragment_obj[3],
>               "Fragment4");
> 
>       fragMentTra.commit();   }

This is how I have added the four fragment to the screen. Now only am looking into view pager. So could you please tell me how can I achieve that with view pager with example code.

My XML File was:

<LinearLayout
    android:id="@+id/upper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/up_left_area"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#ffff66" >

        <FrameLayout
            android:id="@+id/fragment_container1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/up_right_area"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#ccffff" >

        <FrameLayout
            android:id="@+id/fragment_container2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/lower"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/down_left_area"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#66cc33" >

        <FrameLayout
            android:id="@+id/fragment_container3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/down_right_area"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#cc6600" >

        <FrameLayout
            android:id="@+id/fragment_container4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</LinearLayout>

M Vignesh
  • 1,586
  • 2
  • 18
  • 55

1 Answers1

0

You don't need to use FragmentTransactions at all. Have a look at FragmentPagerAdapter if you want to have 4 'swipable' screens with each screen as a Fragment.

The main thing you want is to get the ViewPager from your XML:

mPager = (ViewPager)findViewById(R.id.pager);

Then you want to create your custom FragmentPagerAdapter by extending FragmentPagerAdapter and defining the construction of the fragments:

public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUM_ITEMS; // In this case, 4
    }

    @Override
    public Fragment getItem(int position) {
        // This is basically just making a new Fragment
        // e.g. if (position == 0) return new FirstPageFragment()
        return ArrayListFragment.newInstance(position);
    }
}

Then make the adapter:

mAdapter = new MyAdapter(getSupportFragmentManager());

Then set the adapter on the ViewPager:

mPager.setAdapter(mAdapter);

There are a lot of gotchas with this. For example, the ViewPager handles the setting of fragment tags, so you have to be really careful with Activity recreation (see ViewPager and fragments — what's the right way to store fragment's state? for a discussion on this).

There are also a lot of things you will need to think about. Do you want to keep Fragments if they are off screen? What about if you only want some? Look into FragmentStatePagerAdapter and ViewPager.setOffscreenLimit

Community
  • 1
  • 1
Dororo
  • 3,420
  • 2
  • 30
  • 46
  • No No.. This is not my scenario. I want to divide a single screen into four parts. Fill each part with fragment. I want the consecutive screens also like this. – M Vignesh Jan 19 '13 at 14:05
  • Your answer for the scenario of each screen with single fragment. But for my scenario table, each screen with four fragments. – M Vignesh Jan 19 '13 at 14:06