为了账号安全,请及时绑定邮箱和手机立即绑定

利用ViewPager做向导界面

标签:
Android

布局


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

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始体验"
        android:layout_centerHorizontal="true"
        android:textColor="#f00"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:visibility="invisible" />
</RelativeLayout>

2. 具体逻辑代码



public class GuideActivity extends AppCompatActivity{
    private ViewPager vp_guide;
    private List<ImageView> imageViews;
    private int[] images={R.drawable.guide_1,
            R.drawable.guide_2, R.drawable.guide_3};
    private Button btn_start;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        vp_guide= (ViewPager) findViewById(R.id.vp_guide);
        btn_start= (Button) findViewById(R.id.btn_start);

        initData();
        vp_guide.setAdapter(new VpAdapter());
        vp_guide.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                if(position==imageViews.size()-1){
                    btn_start.setVisibility(View.VISIBLE);
                }else{
                    btn_start.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GotoActivity.gotoActivity(GuideActivity.this,MainActivity.class);
            }
        });
    }

    public void initData(){
        imageViews=new ArrayList<>();
        ImageView view;
        for (int i = 0; i <images.length ; i++) {
            view=new ImageView(this);
            view.setBackgroundResource(images[i]);
            imageViews.add(view);
        }
    }
    class VpAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view=imageViews.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
           container.removeView((View) object);
        }
    }
}

原文链接:http://www.apkbus.com/blog-875916-62823.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消