com.flan.stock.activity.WelcomeActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.flan.stock.activity.WelcomeActivity.java

Source

/*
 *    Flan.Zeng 2011-2016   http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.activity;

import java.util.ArrayList;
import java.util.List;

import com.flan.stock.adapter.WelcomeAdapter;
import com.flan.stock.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * ??
 * @author flan
 * @date   2015105
 */
public class WelcomeActivity extends Activity implements OnPageChangeListener {

    private ViewPager viewPager;
    //?
    private ImageView[] dots;
    //??
    private int currentIndex = 0;

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

        this.setContentView(R.layout.activity_welcome);
        viewPager = (ViewPager) this.findViewById(R.id.viewpager);

        List<View> viewList = getPagers();

        viewPager.setAdapter(new WelcomeAdapter(viewList)); //viewpager?
        //viewPager.setOnPageChangeListener(this);      //viewpager??
        viewPager.addOnPageChangeListener(this);

        //??
        initDot();
    }

    //???
    @SuppressLint("InflateParams")
    private List<View> getPagers() {

        List<View> viewList = new ArrayList<>();
        LayoutInflater inflater = this.getLayoutInflater();

        View pager1 = inflater.inflate(R.layout.item_welcome_a, null);
        //View pager2 = inflater.inflate(R.layout.item_welcome_b, null);
        View pager3 = inflater.inflate(R.layout.item_welcome_c, null);

        Button button = (Button) pager3.findViewById(R.id.but_comein);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //System.out.println("===========>>>> ?Home?");
                Intent intent = new Intent(WelcomeActivity.this, HomeActivity.class);
                WelcomeActivity.this.startActivity(intent);
                WelcomeActivity.this.finish();
            }

        });

        viewList.add(pager1);
        //viewList.add(pager2);
        viewList.add(pager3);

        return viewList;
    }

    //??
    private void initDot() {

        LinearLayout ll = (LinearLayout) this.findViewById(R.id.lay_dot);

        int size = viewPager.getAdapter().getCount();
        dots = new ImageView[size];

        for (int i = 0; i < dots.length; i++) {

            dots[i] = (ImageView) ll.getChildAt(i);
            dots[i].setTag(i);
            //dots[i].setEnabled(true);
            //?
            dots[i].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    int position = (int) v.getTag(); //??

                    setCurrentDot(position);
                    setCurrentView(position);
                }
            });
        }

        dots[currentIndex].setEnabled(false); //??
    }

    //??
    private void setCurrentDot(int position) {

        if (position < 0 || position >= viewPager.getAdapter().getCount() || position == currentIndex) {
            return;
        }

        dots[position].setEnabled(false);
        dots[currentIndex].setEnabled(true);

        currentIndex = position;
    }

    //?????
    private void setCurrentView(int position) {

        if (position < 0 || position > viewPager.getAdapter().getCount()) {
            return;
        }

        viewPager.setCurrentItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

        //state 
        //SCROLL_STATE_IDLE      ?????
        //SCROLL_STATE_DRAGGING   ?
        //SCROLL_STATE_SETTLING ????
        //??????
        //System.out.println("=========>>>>> "+state);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        //position??
        //positionOffset????????1???0???????0
        //positionOffsetPixels????positionOffset
        //System.out.println("=========>>>>> "+position +" | "+positionOffset+" | "+positionOffsetPixels);
    }

    //???
    @Override
    public void onPageSelected(int position) {
        setCurrentDot(position);
    }

}