Android swiping, using ViewPager without tabs
Asked Answered
R

2

15

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 ?

Retake answered 4/4, 2013 at 12:41 Comment(1)
P
17

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

}
Ponce answered 4/4, 2013 at 12:55 Comment(8)
Thanks so much ! I have been looking around for 2 days now, for a solution to my problem ! :)Retake
But what if i wat to start at case 2 ? Should i just take the content of case 0 and put it in case 2 ?Retake
That depends, do you want to keep the items in the same position? If keeping each fragment in the same location is not of concern then you can switch them. Otherwise in your MainActivity you would use pagerAdapter.setCurrentItem(1); where 1 is equal to the position you want, remember 1 is the second page. Do this before pager.setAdapter(pagerAdapter);Ponce
I'm sorry I meant pager.setCurrentItem(1);. See my latest edit for reference.Ponce
One more correction, see edit, do pager.setCurrentItem(1); after setAdapter(). Sorry about that.Ponce
What about the transition between activitys ?Retake
I'm not sure I understand, what do you mean 'transition between activities'?Ponce
@Asok can i use the viewpager in both direction from left to right and right to left!! here is my question https://mcmap.net/q/824781/-viewpager-in-androidHue
A
0

I have added this in fragment class ..

the issue i got is cannot resolve cannot resolve get support fragment manager ().

@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);

}
Alephnull answered 18/11, 2016 at 6:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.