Lasse Lasse - 21 days ago 7
Android Question

Android swiping, using ViewPager without tabs

Im trying to make an app that have the navigation type of swiping.
This is how far I have gone:

Fragment activity:

package com.app.BoomBase;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment_control extends Fragment {

String tag = this.getClass().getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(tag, "onCreate");
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(tag, "onCreateView");

View view = inflater.inflate(R.layout.fragment_controle,container, false );

return view;
}

}


FragmentPageAdapter:

package com.app.BoomBase;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class Fragment_Pager extends FragmentPagerAdapter {

final int PAGE_COUNT = 3;
public Fragment_Pager(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {

Fragment_control myFragment = new Fragment_control();
Bundle data = new Bundle();
data.putInt("current_page", arg0+1);
myFragment.setArguments(data);
return myFragment;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}

}


MainActivity:

package com.app.BoomBase;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

String tag = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(tag, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager pager = (ViewPager) findViewById(R.id.pager);

/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();

/** Instantiating FragmentPagerAdapter */
Fragment_Pager pagerAdapter = new Fragment_Pager(fm);

/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);

}
}


But my problem is that i can't figure out how to add fragments to the code. I want to swipe to the next activity with buttons and stuff on them. How do I do that ?

Answer

PageAdapter uses your getItem() to switch between Fragments, this is where you would declare which page does what. "Position" 0 is the first page, 1 is the second and so forth. You can simply return a new instance of your other Fragments or pass arguments if you wish.

For example:

@Override
public Fragment getItem(int position) {
    switch (position) {
    case 0:
        // Your current main fragment showing how to send arguments to fragment
        Fragment_control myFragment = new Fragment_control();
        Bundle data = new Bundle();
        data.putInt("current_page", position+1);
        myFragment.setArguments(data);
        return myFragment;
    case 1:
        // Calling a Fragment without sending arguments
        return new MySecondFragment();
    case 2:
        return new MyThirdFragment();
    default:
        return null;
    }
}

Then you would create a Fragment class for each of those you want to incorporate. In my example you would have a class for MySecondFragment and MyThirdFragment

public class MySecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.second_fragment, null); 
        return view;
    }

}

And

public class MyThirdFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.third_fragment, null); 
        return view;
    }

}

Any fragment may simply inflate a standard xml layout file, to access the children in the layout you would have to remember to use the following, I'll use MySecondFragment as an example.

Let's say you have two Buttons with id's R.id.button1 and R.id.button2 in a layout file titled 'second_fragment':

public class MySecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.second_fragment, null);

        // Make sure to add the parent inflater before and layout child call
        Button btn_one = (Button)view.findViewById(R.id.button1);
        Button btn_two = (Button)view.findViewById(R.id.button2);

        return view;
    }

}

Edit

To start at a page other then 0 or the first position you would simply use setCurrentItem() in your MainAvticity, like so:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(tag, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();
    Fragment_Pager pagerAdapter = new Fragment_Pager(fm);
    // Here you would declare which page to visit on creation
    pager.setAdapter(pagerAdapter);
    pager.setCurrentItem(1);

}