0

I am new to Android development. I am creating an application with Fragments. Following is the Diagram.

enter image description here

Home screen is having the root access to all the other screens. Sales is a ViewPager and all the other screens after sales are part of it. User can slide and access.

But I do have a problem. I am planning to use one FragmentActivity for this application. It is the Home. Home screen is a collection of buttons, with images. If the user click on 'Sales' button then the Home screen flips just like ViewPager animation and access Sales screen; if the user click on 'TimeManager' button then the Home screen flips just like ViewPager animation and access TimeManager screen and so on.

Following is the code of the HomeScreen

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.home);

    }

Now imagine I am in Sales screen. As I mentioned earlier, it has sequence of pages (New Lead and EditInfo) where the user can access by sliding. So it should be a ViewPager.

Based on the above information, I have to ask 2 questions.

  1. While in Home screen, how user can click on the button and make the screen slide to access the next screen? When the user is in next screen (ex: To Do List) I prefer to let him slide right to go back to the Home screen. If this is not possible, at least pressing back button should do the sliding. How can I do this?

  2. This contains application only one FragmentActivity and the FragmentActivity class contains the UI of Home screen. So where should I put the ViewPager of Sales screen? Sales is a Fragment and not FragmentActivity. So how can I facilitate it to navigate to NewLead and Edit Info pages via ViewPager?

Please help.

UPDATE

Since I am adviced to use a Fragment for ViewPager I would like to know whether it is best to create a seperate fragment for ViewPager. Below is the code. What is your advice?

public class ViewPagerManager extends Fragment{

    private static final int ITEMS = 2;
    private ViewPager viewPager;
    private MyAdapter pageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_leads_and_sales_handling);

        viewPager = (ViewPager)findViewById(R.id.pager);
        pageAdapter = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pageAdapter);
    }



private  class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            if(position==0)
            {
                return new SalesInqury();
            }
            else
            {
                return new NewLead();
            }
        }
    }
}
Dongle
  • 602
  • 1
  • 8
  • 18

1 Answers1

0

You can change the animation used when you switch between the fragments from your home screen using the android.animation framework. Some more information can be found here: Animate the transition between fragments

In the sales fragment you will have a ViewPager as you suggested and this ViewPager will contain 3 more Fragments one will be for Sales one will be for New Lead and one will be for Edit info. Hopefully that makes sense to you

Community
  • 1
  • 1
Dreagen
  • 1,733
  • 16
  • 21
  • No. 'Sales' class is Sales.java, 'New Lead' is NewLead.java and so on. ViewPager should be implemented in FragmentActivity, not in Fragment right? All those screens are Fragments. – Dongle Jan 13 '14 at 13:11
  • You can use a ViewPager where ever you want. It doesn't have to necessarily be in a FragmentActivity. If you want Sales, New Lead and Edit Info to be part of a View Pager then they should be Fragments. You then create a ViewPager object and add your 3 fragments to it – Dreagen Jan 13 '14 at 13:14
  • Then should I create a separate Fragment to hold the viewpager? I have updated a sample code, please have a look. – Dongle Jan 13 '14 at 13:37
  • Yes, what you have seems fine. Is it working ok? To make it easier for you could use a SlidePagerAdapter for your view pager. So viewPager.setAdapter(new SlidePagerAdapter(activity.getFragmentManager(), fragment1, fragment2, fragment3) – Dreagen Jan 13 '14 at 13:46
  • I don't know. It was a quick workout. I didn't check. Will let you know soon. – Dongle Jan 13 '14 at 13:47
  • In my previous comment fragment1, fragment2, and fragment3 would be created by Fragment fragment1 = new SalesInquiry() etc – Dreagen Jan 13 '14 at 13:48