如何在Android中显示的图像和向右滑动即可更改/剩下什么?

问题描述:

我要介绍添加到我的Andr​​oid应用程序,来通知应用程序是如何工作的用户。这个片头只显示,如果preferred设置片头是假的。因此,在此介绍,将有3个图像,并在结束时,将有一个页面,其中一些文本和两个按钮,以使用户能够访问应用程序,通过使一个登录。每个图像之间的变化,将与刷卡动作,进行(左+右如此,从左至右 - )。我该怎么办?

I want to add an introduction to my Android application, to inform the user about how the app works. This intro will be displayed only, if the preferred settings intro will be false. So in this intro, there will be 3 images and at the end, there will be a page, with some text and two buttons, to enable the user to access the application, by making a login. The change between each image, will be made with a swipe movement, (so right to left +, left to right -). How Can I do ?

这可以通过使用片段和ViewPager和FragmentPagerAdapter完成的。看看这个文档:

This can be done via the use of Fragments and ViewPager and FragmentPagerAdapter. Look at this documentation:

FragmentPagerAdapter:的http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

FragmentPagerAdapter: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

ViewPager:
http://developer.android.com/reference/android/support /v4/view/ViewPager.html

ViewPager: http://developer.android.com/reference/android/support/v4/view/ViewPager.html

可以有一个基于在ViewPager的ID实例一个片段,并且该id将表明在图像片段要显示的图像。因此,对于三张图片,实例化,设置在设在FragmentPagerAdapter当前页面上的片段图像的新片段。第二个片段可能是一个你想在年底的登录按钮和文本。

You can have one fragment that is instantiated based on the id in the ViewPager, and that id will indicate which image to show in your image fragment. So for three images, you instantiate a new fragment that sets the image in the fragment based on the current page in the FragmentPagerAdapter. The second fragment can be one for the login buttons and text you want at the end.

前在您FragmentActivity定义的适配器(或AppCompatActivity)

Ex for adapter defined in your FragmentActivity (or AppCompatActivity)

    public class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

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

        @Override
        public Fragment getItem(int position) {
            if(position < 3)
                return ImageFragment.newInstance(position);
            else
                return new LoginFragment();
        }
}

前为你介绍各种图像的图像片段:

Ex for the image fragment for the various images in your introduction:

public static class ImageFragment extends Fragment{
    private int mPosition;

    public ImageFragment(){
    }

    public static ImageFragment newInstance(int pos){
        ImageFragment frag = new ImageFragment();
        Bundle args = new Bundle();
        args.putInt("pos", pos);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPosition = getArguments().getInt("pos");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_image, container, false);
            ImageView backgroundView = (ImageView) v.findViewById(R.id.background_image);
            switch(mPosition){
                case 0:
                    //set background view image 1
                case 1:
                    //set background view image 2
                default:
                    //set background view image 3
            }
            return v;
    }

}