第八天案例+fragment+viewPager(引导页)+小圆点+跳转

fragment+viewPager引导页+小圆点+跳转

      • 思路
      • Activity中的代码
      • 第三个fragment中的代码

思路

1,viewpager 和fragment 做引导页
2,创建三个fragment,最后一个里面添加跳转按钮和倒计时
3,fragment和activity用handler通讯
4,主要页面逻辑在welcomeActivity和第三个fragment里面.

Activity中的代码

package com.example.day008.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.day008.MainActivity;
import com.example.day008.R;
import com.example.day008.adapter.MyAdapter;
import com.example.day008.fragment.WelcomeFragment;
import com.example.day008.fragment.WelcomeFragmentThree;
import com.example.day008.fragment.WelcomeFragmentTwo;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class WelcomeActivity extends AppCompatActivity {
     
    private ViewPager vp;
    private static final String TAG = "WelcomeActivity";
    private ArrayList<Fragment> welcomeFragments;
    private int index =0;
    private LinearLayout ll;
    private Timer timer;
    private Handler handler = new Handler(){
     
        @Override
        public void handleMessage(Message msg) {
     
            super.handleMessage(msg);
            if(msg.what == 100){
     
                vp.setCurrentItem(index);
                index++;
                //停止自动翻页
                if(index == welcomeFragments.size()){
     
                    timer.cancel();
                }
            }
        }
    };
    private WelcomeFragmentThree welcomeFragmentThree;
    private ArrayList<ImageView> imageViews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        SharedPreferences welcome = getSharedPreferences("welcome", Context.MODE_PRIVATE);
        boolean welcome1 = welcome.getBoolean("welcome", false);
        if(welcome1){
     
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            //finish后当前的activity不会立即结束,还是用执行完oncreate的内容.所以要return;
            finish();
            return;
        }


        vp = (ViewPager) findViewById(R.id.vp);
        ll = (LinearLayout) findViewById(R.id.ll);


        welcomeFragments = new ArrayList<>();
        welcomeFragments.add(new WelcomeFragment());
        welcomeFragments.add(new WelcomeFragmentTwo());
        welcomeFragmentThree = new WelcomeFragmentThree();
        welcomeFragments.add(welcomeFragmentThree);

        imageViews = new ArrayList<>();
        for (int i = 0; i <welcomeFragments.size() ; i++) {
     
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.circle_false);
            if(i == 0){
     
                imageView.setImageResource(R.drawable.circle_true);
            }
            imageViews.add(imageView);
            ll.addView(imageView);
        }
        vp.setAdapter(new MyAdapter(getSupportFragmentManager(),welcomeFragments));

        SharedPreferences welcome2 = getSharedPreferences("welcome", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = welcome2.edit();
        edit.putBoolean("welcome",true);
        edit.commit();

        timer = new Timer();
        timer.schedule(new TimerTask() {
     
            @Override
            public void run() {
     
                handler.sendEmptyMessage(100);
            }
        },0,1500);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
     
            @Override
            public void onPageScrolled(int i, float v, int i1) {
     

            }

            @Override
            public void onPageSelected(int i) {
     

                if(i == welcomeFragments.size()-1){
     
                    welcomeFragmentThree.handler.sendEmptyMessage(110);
                }
                for (int j = 0; j < imageViews.size(); j++) {
     
                    if(j == i){
     
                        imageViews.get(j).setImageResource(R.drawable.circle_true);
                    }else{
     
                        imageViews.get(j).setImageResource(R.drawable.circle_false);
                    }

                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {
     

            }
        });

    }
}

Activity中的xml布局




    
    

    

    



Adapter的中的代码

package com.example.day008.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import java.util.List;

public class MyAdapter extends FragmentStatePagerAdapter {
     

    private List<Fragment> list;

    public MyAdapter(FragmentManager fm, List<Fragment> list) {
     
        super(fm);
        this.list = list;
    }

    @Override
    public Fragment getItem(int i) {
     
        return list.get(i);
    }

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

第三个fragment中的代码

package com.example.day008.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.day008.MainActivity;
import com.example.day008.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class WelcomeFragmentThree extends Fragment {
     
    private TextView time;
    private Timer timer;
    private int count =5;
    private static final String TAG = "WelcomeFragmentThree";
    @SuppressLint("HandlerLeak")
    public Handler handler = new Handler(){
     
        @Override
        public void handleMessage(Message msg) {
     
            super.handleMessage(msg);
            if(msg.what == 110){
     
                Log.i(TAG, "handleMessage: "+msg.what);
                timer = new Timer();
                timer.schedule(new TimerTask() {
     
                    @Override
                    public void run() {
     
                        handler.sendEmptyMessage(111);
                    }
                },0,1000);


            }else if(msg.what == 111){
     
                count--;
                Log.i(TAG, "handleMessage: 1111"+msg.what);
                time.setText("倒计时"+count+"秒");
                if(count == 0){
     
                    timer.cancel();
                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }
            }
        }
    };

    private Button btn;
    public WelcomeFragmentThree() {
     
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_welcome_fragment_three, container, false);


        time = (TextView) inflate.findViewById(R.id.time);
        btn = (Button) inflate.findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
     
            @Override
            public void onClick(View view) {
     
                timer.cancel();
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });

        return inflate;
    }
}

fragment中的xml布局



    

    

    

另外两个fragment就是默认创建给你图片即可

package com.example.day008.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.day008.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class WelcomeFragment extends Fragment {


    public WelcomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_welcome, container, false);
    }

}

你可能感兴趣的:(Android从零开始)