Using ViewPager for Screen Slides 使用屏幕幻灯片ViewPager
来源:互联网 发布:mmd动作数据下载网站 编辑:程序博客网 时间:2024/05/18 00:05
Screen slides are transitions between one entire screen to another and are common with UIs like setup wizards or slideshows. This lesson shows you how to do screen slides with aViewPager
provided by the support library.ViewPager
s can animate screen slides automatically. Here's what a screen slide looks like that transitions from
If you want to jump ahead and see a full working example, download and run the sample app and select the Screen Slide example. See the following files for the code implementation:
src/ScreenSlidePageFragment.java
src/ScreenSlideActivity.java
layout/activity_screen_slide.xml
layout/fragment_screen_slide_page.xml
Create the Views
Create a layout file that you'll later use for the content of a fragment. The following example contains a text view to display some text:
<com.example.android.animationsdemo.ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView style="?android:textAppearanceMedium" android:padding="16dp" android:lineSpacingMultiplier="1.2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/lorem_ipsum" /></com.example.android.animationsdemo.ScrollView>
Create the Fragment
Create a Fragment
class that returns the layout that you just created in theonCreateView()
method. You can then create instances of this fragment in the parent activity whenever you need a new page to display to the user:
public class ScreenSlidePageFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup rootView = (ViewGroup) inflater.inflate( R.layout.fragment_screen_slide_page, container, false); return rootView; }}
Screen Slides with ViewPager
ViewPager
s have built-in swipe gestures to transition through pages, and they display screen slide animations by default, so you don't need to create any.ViewPager
s use PagerAdapter
s as a supply for new pages to display, so thePagerAdapter
will use the fragment class that you created earlier.
To begin, create a layout that contains a ViewPager
:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" />
Create an activity that does the following things:
- Sets the content view to be the layout with the
ViewPager
. - Create a class that extends the
FragmentStatePagerAdapter
abstract class. Implement thegetItem()
method to supply instances ofScreenSlidePageFragment
as new pages. The pager adapter also requires that you implement thegetCount()
method, which returns the amount of pages the adapter will create (five in the example). - Hooks up the
PagerAdapter
to theViewPager
. - Handle's 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.
public class ScreenSlidePagerActivity extends FragmentActivity { /** * The number of pages (wizard steps) to show in this demo. */ private static final int NUM_PAGES = 5; /** * The pager widget, which handles animation and allows swiping horizontally to access previous * and next wizard steps. */ private ViewPager mPager; /** * The pager adapter, which provides the pages to the view pager widget. */ private PagerAdapter mPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_screen_slide_pager); // Instantiate a ViewPager and a PagerAdapter. mPager = (ViewPager) findViewById(R.id.pager); mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager()); mPager.setAdapter(mPagerAdapter); } @Override public void onBackPressed() { if (mPager.getCurrentItem() == 0) { // If the user is currently looking at the first step, allow the system to handle the // Back button. This calls finish() on this activity and pops the back stack. super.onBackPressed(); } else { // Otherwise, select the previous step. mPager.setCurrentItem(mPager.getCurrentItem() - 1); } } /** * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in * sequence. */ private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { public ScreenSlidePagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return new ScreenSlidePageFragment(); } @Override public int getCount() { return NUM_PAGES; } }}
- Using ViewPager for Screen Slides 使用屏幕幻灯片ViewPager
- Using ViewPager for Screen Slides
- adding animations - Using ViewPager for Screen Slides
- Adding Animations之Using ViewPager for Screen Slides
- 使用ViewPager屏幕幻灯片
- Android Animations(二):使用ViewPager实现多屏滑动(Screen Slides)
- android学习(十八) 添加动画(二) 使用ViewPager的屏幕幻灯片
- 使用ViewPager实现幻灯片播放功能
- Android官网培训课:用ViewPager做屏幕幻灯片
- Android:ViewPager制作幻灯片
- Android:ViewPager制作幻灯片
- 使用ViewPager实现屏幕滑动
- Android使用ViewPager来滑动屏幕
- 使用ViewPager实现屏幕侧滑
- viewpager使用
- ViewPager使用
- ViewPager使用
- ViewPager使用
- 1063 两字符比大小
- 将Label的Caption内容竖向显示
- Ucos学习之写在开头
- 【道森试听】执行计划+光标移动大法——落落
- SQL SERVER 插入、取回
- Using ViewPager for Screen Slides 使用屏幕幻灯片ViewPager
- Android消息机制
- 学习mysql(5) function 创建
- twisted学习笔记
- c++高级---图解:C++中类的内存分布(总结)
- DS18B20多ROM搜索并读取温度
- 1064 三数求大值
- oracle 28000错误解决方法
- 拾取之选择模式