Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using ViewPager for Screen Slides in Android

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.52k
    Comment on it

    ViewPagers have built-in swipe gestures to transition through pages

    You don't need to add any animation.

    sample code :

    Set your main Activity layout as:

    in mainactivity Layout xml file set relative layout,in it set the view pager.: 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">
    
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    </RelativeLayout>

     

     

    MainActivity:

    Create an activity that does the following things:

    1. Sets the content view to be the layout with the ViewPager.

    -Creates a class that extends the FragmentStatePagerAdapter abstract class and implements the getItem() method to supply instances of ScreenSlidePageFragment as new pages. The pager adapter also requires that you implement the getCount() method, which returns the amount of pages the adapter will create (five in the example). - Hooks up the PagerAdapter to the ViewPager. Handles the device's back button by moving backwards in the virtual stack of fragments. If the user is already on the first page, go back on the activity back stack.

     

    package com.example.viewpager;
    
    import java.util.ArrayList; 
    import java.util.List;
    
    import android.support.v4.app.Fragment;
    
    import android.os.Bundle; import android.support.v4.app.FragmentActivity; 
    import android.support.v4.app.FragmentPagerAdapter; 
    import android.support.v4.app.FragmentManager; 
    import android.support.v4.view.ViewPager;
    
    public class MainActivity extends FragmentActivity 
    {
    
    MyPageAdapter pageAdapter; 
    @Override public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    List<Fragment> fragments = getFragments(); 
    pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments); 
    ViewPager pager = (ViewPager) findViewById(R.id.viewpager); 
    pager.setAdapter(pageAdapter); 
    } 
    private List<Fragment> getFragments() 
    { 
    List<Fragment> fList = new ArrayList<Fragment>(); 
    fList.add(MyFragment.newInstance("Fragment 1")); 
    fList.add(MyFragment.newInstance("Fragment 2")); 
    fList.add(MyFragment.newInstance("Fragment 3")); 
    return fList; 
    } 
    private class MyPageAdapter extends FragmentPagerAdapter 
    { private List<Fragment> fragments; 
    public MyPageAdapter(FragmentManager fm, 
    List<Fragment> fragments) 
    { 
    super(fm); this.fragments = fragments; 
    } 
    @Override public Fragment getItem(int position) 
    { 
    return this.fragments.get(position); 
    } 
    @Override public int getCount() 
    { 
    return this.fragments.size(); 
    } }
    
    }
    

     

    FragmentActivity: 

    package com.example.viewpager;
    
    import android.os.Bundle; import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View; import android.view.ViewGroup;
    import android.widget.TextView;
    
    public class MyFragment extends Fragment
    {
    public static final String EXTRAMESSAGE = "EXTRAMESSAGE";
    
    public static final MyFragment newInstance(String message)
    {
    MyFragment f = new MyFragment();
    Bundle bdl = new Bundle(1);
    bdl.putString(EXTRA_MESSAGE, message);
    f.setArguments(bdl);
    return f;
    }
    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    String message = getArguments().getString(EXTRA_MESSAGE);
    View v = inflater.inflate(R.layout.myfragment_layout, container, false);
    TextView messageTextView = (TextView)v.findViewById(R.id.textView1); messageTextView.setText(message); return v; }
    
    }

    You can take additional help from:

    http://developer.android.com/training/animation/screen-slide.html

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: