1. 程式人生 > >引導頁小圓點滑動

引導頁小圓點滑動

這裡寫圖片描述 這裡寫圖片描述 這裡寫圖片描述

佈局程式碼


    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
     <LinearLayout
         android:layout_marginBottom="12dp"
         android:layout_alignParentBottom="true"
         android:gravity
="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<ImageView android:id="@+id/iv_one_dot" android:layout_width="30dp" android:layout_height
="30dp" android:src="@drawable/have_dot" />
<ImageView android:id="@+id/iv_two_dot" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/no_dot" /> <ImageView android:layout_width="30dp"
android:layout_height="30dp" android:id="@+id/iv_three_dot" android:src="@drawable/no_dot" />
</LinearLayout> </RelativeLayout>

佈局程式碼分別是一個容器viewPager和三個小圓點圖片

MainActivity程式碼


public class MainActivity extends AppCompatActivity {

    private ViewPager viewpager;
    private ImageView image1;
    private ImageView image2;
    private ImageView imageView;
    private View view1;
    private View view2;
    private View view3;
    private ArrayList<View> list;
    private ImageView iv_one_dot;
    private ImageView iv_two_dot;
    private ImageView iv_three_dot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //隱藏狀態列
        Window window = getWindow();
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        window.setFlags(flag, flag);

        setContentView(R.layout.activity_main);
        //初始化檢視
        initView();

        //隱藏標題欄
        ActionBar ActionBar = getSupportActionBar();
        if (ActionBar!=null){
            ActionBar.hide();
        }
    }

    private void initView() {
        viewpager = findViewById(R.id.viewpager);
        iv_one_dot = findViewById(R.id.iv_one_dot);
        iv_two_dot = findViewById(R.id.iv_two_dot);
        iv_three_dot = findViewById(R.id.iv_three_dot);

        list = new ArrayList<>();

        /* 例項化三個引導頁佈局 
        這裡就不貼上程式碼了,無非就是三個佈局和三個圖片控制元件
        注:在ImageView需設定android:scaleType="fitXY" 意思是圖片進行全屏填充*/ 
        view1 = View.inflate(this, R.layout.guide_one, null);
        view2 = View.inflate(this, R.layout.guide_two, null);
        view3 = View.inflate(this, R.layout.guide_three, null);
        list.add(view1);
        list.add(view2);
        list.add(view3);

        //滑動頁面監聽事件
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //小圓點滑動監聽事件
            @Override
            public void onPageSelected(int position) {
                Log.i("MainActivity", String.valueOf(position));
                if (position==0){
                    iv_one_dot.setImageResource(R.drawable.have_dot);
                    iv_two_dot.setImageResource(R.drawable.no_dot);
                    iv_three_dot.setImageResource(R.drawable.no_dot);
                }if (position==1){
                    iv_one_dot.setImageResource(R.drawable.no_dot);
                    iv_two_dot.setImageResource(R.drawable.have_dot);
                    iv_three_dot.setImageResource(R.drawable.no_dot);
                }if (position==2){
                    iv_one_dot.setImageResource(R.drawable.no_dot);
                    iv_two_dot.setImageResource(R.drawable.no_dot);
                    iv_three_dot.setImageResource(R.drawable.have_dot);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //viewpager介面卡
        viewpager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

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

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(list.get(position));
                return list.get(position);
            }

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