zoukankan      html  css  js  c++  java
  • Android中ViewPager的使用

    我们在一个apk中第一次開始的时候,会有一个guide界面。一般使用ViewPager来完毕。

    布局文件例如以下:

    <?

    xml version="1.0" encoding="utf-8"?> <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/guide_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> <Button android:id="@+id/guide_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:text="開始体验" android:textSize="20sp" android:visibility="gone" /> </RelativeLayout>

    这就是一个想到Activity GuideActivity.java

    package com.xlg.news;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.xlg.news.adapters.WelcomeViewPagerAdapter;
    import com.xuliugen.newsclient.R;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.opengl.Visibility;
    import android.os.Bundle;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class GuideActivity extends Activity {
    
        private ViewPager guideViewPager;
        private Button guideButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.guide_activity);
            guideViewPager = (ViewPager) this.findViewById(R.id.guide_viewpager);
            guideButton = (Button) this.findViewById(R.id.guide_button);
    
            //获取图片ImageView资源
            final List<ImageView> imageViewsList = initData();
    
            WelcomeViewPagerAdapter viewPagerAdapter = new WelcomeViewPagerAdapter(GuideActivity.this,imageViewsList);
            guideViewPager.setAdapter(viewPagerAdapter);
    
            /**
             * 设置guide ViewPager滑动的时候的监听事件
             */
            guideViewPager.setOnPageChangeListener(new OnPageChangeListener() {
    
                /**
                 * 已经选中某一页的时候
                 */
                @Override 
                public void onPageSelected(int position) {
                    //已经到了最后一页
                    if(position == imageViewsList.size()-1){
                        guideButton.setVisibility(View.VISIBLE);
                        guideButton.setOnClickListener(new OnClickListener() {
    
                            @Override
                            public void onClick(View v) {
                                //进入最后一页设置向导页,点击按钮完毕功能
                            }
                        });
                    }else{
                        guideButton.setVisibility(View.GONE);
                    }
                }
    
                /**
                 * 
                 * 在页面的滑动过程中
                 */
                @Override
                public void onPageScrolled(int position, float arg1, int arg2) {
    
                }
    
                /**
                 * 
                 * 滑动状态发生改变的状态
                 */
                @Override
                public void onPageScrollStateChanged(int position) {
    
                }
            });
        }
    
        private List<ImageView> initData() {
            List<ImageView> imageViewsList = new ArrayList<ImageView>();
            // 加入图片控件
            ImageView imageView1 = new ImageView(getApplicationContext());
            imageView1.setBackgroundResource(R.drawable.guide_image1);
            ImageView imageView2 = new ImageView(getApplicationContext());
            imageView2.setBackgroundResource(R.drawable.guide_image2);
            ImageView imageView3 = new ImageView(getApplicationContext());
            imageView3.setBackgroundResource(R.drawable.guide_image3);
            imageViewsList.add(imageView1);
            imageViewsList.add(imageView2);
            imageViewsList.add(imageView3);
            return imageViewsList;
        }
    
    }
    

    当中的适配器例如以下:

    package com.xlg.news.adapters;
    
    import java.util.List;
    
    import android.content.Context;
    import android.support.v4.view.PagerAdapter;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    
    /**
     * 第一次启动页面的ViewPager适配器
     * @author xuliugen
     */
    public class WelcomeViewPagerAdapter extends PagerAdapter {
    
        private Context mContext;
        private List<ImageView> mImageViewsList;
    
        public WelcomeViewPagerAdapter(Context context,
                List<ImageView> imageViewsList) {
            mContext = context;
            mImageViewsList = imageViewsList;
        }
    
        @Override
        public int getCount() {
            return mImageViewsList.size();
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    
        /**
         * 给制定的viewpager加入一个view的方法
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //加入一个imageView
            container.addView(mImageViewsList.get(position));
            return mImageViewsList.get(position);
        }
    
        /**
         * 给制定的viewpager移除一个view的方法
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }
    }
    
  • 相关阅读:
    [LeetCode] 1131. Maximum of Absolute Value Expression 绝对值表达式的最大值
    [LeetCode] 1130. Minimum Cost Tree From Leaf Values 叶值的最小代价生成树
    [LeetCode] 1129. Shortest Path with Alternating Colors 颜色交替的最短路径
    [LeetCode] 1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量
    [LeetCode] 1125. Smallest Sufficient Team 最小的必要团队
    [LeetCode] 1124. Longest Well-Performing Interval 表现良好的最长时间段
    [LeetCode] 1122. Relative Sort Array 数组的相对排序
    Gitalk 自动初始化评论
    [LeetCode] 1111. Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度
    [LeetCode] 1110. Delete Nodes And Return Forest 删点成林
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7072442.html
Copyright © 2011-2022 走看看