• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Creating SwipeView

gazzieh

Newbie
I have the beginnings of an app that will eventually comprise of three screens. I want to allow the user to move through the screens by flicking the current screen.

The screens should be positioned horizontally: 1 - 2 - 3

So, a flick from right to left should move from 1 to 2, a flick from left to right should move from 3 to 2 and 2 should be able to move to pages 1 and 3 respectively.

I believe this is called SwipeView but cannot find any clear instructions on how to implement this.

May I ask for help please?
 
Here is an example of using a ViewPager to get your "swipe" result.

LAYOUT
Make sure your layout is similar to this and has the ViewPager.
[HIGH=JAVA]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/body_background">

<include layout="@layout/pagerbar" />
<include layout="@layout/colorstrip" />

<android.support.v4.view.ViewPager
android:id="@+id/example_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />

</LinearLayout>
[/HIGH]


ACTIVITY
Setup your PagerAdapter in your "FragmentActivity" and make sure you implement "OnPageChangeListener". Then properly setup your PagerAdapter in your onCreate.
[HIGH=JAVA]
public class Activity extends FragmentActivity implements ViewPager.OnPageChangeListener {
...

public void onCreate(Bundle savedInstanceState) {

PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.example_pager);

pager.setAdapter(adapter);
pager.setOnPageChangeListener(this);
pager.setCurrentItem(MyFragment.PAGE_LEFT);

...
}


/* setup your PagerAdapter which extends FragmentPagerAdapter */

static class PagerAdapter extends FragmentPagerAdapter {
public static final int NUM_PAGES = 2;

private MyFragment[] mFragments = new MyFragment[NUM_PAGES];

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

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

@Override
public Fragment getItem(int position) {
if (mFragments[position] == null) {
/* this calls the newInstance from when you setup the ListFragment */
mFragments[position] = MyFragment.newInstance(position);
}

return mFragments[position];
}
}

...
[/HIGH]


FRAGMENT
When you setup your actual ListFragment (your listViews) you can create multiple instances with arguments like the following:
[HIGH=JAVA]
public static final int PAGE_LEFT = 0;
public static final int PAGE_RIGHT = 1;

static MyFragment newInstance(int num) {
MyFragment fragment = new MyFragment();

Bundle args = new Bundle();
args.putInt("num", num);

fragment.setArguments(args);
return fragment;
}
[/HIGH]
When you reload the listViews (how ever you decide to implement this) you can figure out which fragment instance you are on using the arguments like so:
[HIGH=JAVA]
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
[/HIGH]
If you step through your code you will notice that it will step through each instance so the above code is only needed in your onCreate and a reload method may look like this:

[HIGH=JAVA]
private void reloadFromArguments() {

/* mNum is a variable which was created for each instance in the onCreate */

switch (mNum)
{
case PAGE_LEFT:
/* maybe a query here would fit your needs? */
break;

case PAGE_RIGHT:
/* maybe a query here would fit your needs? */
break;
}
}
[/HIGH]
Few Sources that may help you out with examples that you could build from rather then starting from scratch:

More explanation and examples:
Peter Kuterna's blog: ViewPager meets Swipey Tabs
...which is references to:
android-playground - Android sample applications and projects - Google Project Hosting

More info and some good linkage:
Responsive mobile design on Android: from view pager to action bar tabs
 
Thanks for this Steve.

If I may ask for a little clarity for a newbie, I assume the layout part (first part you gave) goes at the top of each of the activity XML documents I have (I have 4, one for each page)?

I am not too sure what you mean by: Setup your PagerAdapter in your "FragmentActivity". Do I need to create a new Java page for this?

I then fall apart at the FRAGMENT part: I have no idea about listView?

Sorry to be so thick, I really am at stage 0 here.
 
I went through all those sites and with those and another one I found allowed me to create the basis of a SwipeView site. Having a number of issues currently but solving them slowly. Have posted about one of the major ones but thank you for all the help given.
 
Back
Top Bottom