博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using ViewPager for Screen Slides 使用屏幕幻灯片ViewPager
阅读量:4046 次
发布时间:2019-05-24

本文共 4095 字,大约阅读时间需要 13 分钟。

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 a provided by the . s can animate screen slides automatically. Here's what a screen slide looks like that transitions from  

 http://blog.csdn.net/sergeycao

If you want to jump ahead and see a full working example, 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:

Create the Fragment

Create a class that returns the layout that you just created in the 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

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. s use s as a supply for new pages to display, so the will use the fragment class that you created earlier.

To begin, create a layout that contains a :

Create an activity that does the following things:

  • Sets the content view to be the layout with the .
  • Create a class that extends the abstract class. Implement the method to supply instances of ScreenSlidePageFragment as new pages. The pager adapter also requires that you implement the method, which returns the amount of pages the adapter will create (five in the example).
  • Hooks up the to the .
  • 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;        }    }}
你可能感兴趣的文章
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
SQL 多表联合查询
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
GitHub 万星推荐:黑客成长技术清单
查看>>
可以在线C++编译的工具站点
查看>>
关于无人驾驶的过去、现在以及未来,看这篇文章就够了!
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>