Android利用viewpage建立引导页面

主要思想:

             1.建一个StartActivity作为入口。此页面主要任务,显示Splash页面 和 判断是否第一次进入此app,至跳转至引导页面。

                         1.1初始化后,在新线程沉睡1500,后发消息到handler.

                         1.2hander判断是否第一次进入。

                                    1.2.1 第一次,则跳转MainActivity。

                                    1.2.2以后,则跳转到FrameActivity。

            2.如果 IsFirstRun=true 跳转到 MainActivity,此页面主要有一个v4包下的viewpage,实现引导。

                        2.1找到viewpage

                        2.2对viewpage添加Adapter

                                    2.2.1创建FragmentPagerAdapter子类,并实现必须实现的3个方法。

                                                2.2.1.1构造器。

                                                2.2.1.2getItem(int position)方法。返回得到相应位置下的Fragment。

                                                2.2.1.3实现得到Fragment总数,getCount()

            3.由以上2.2.1.2我们需要Fragment子类,将实现的子类放到viewpage。

                        3.1实现onCrateView()方法,此方法在Fragment创建view创建时调用。

                                    3.1.1对view需要填充。View.inflate(context, resource, root); 需要context。所以,Fragment子类构造器必须添加context.

                                    3.1.2return view

                        3.2对构造器添加context


主要代码如下:


/**
 * 软件启动界面
 * */
public class StartActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_my01);
		LinearLayout mLinear = (LinearLayout) findViewById(R.id.Fragment01Linear);
		mLinear.setBackgroundResource(R.drawable.ic_splash_screen);

		new Thread() {
			public void run() {
				try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Message msg = hand.obtainMessage();
				hand.sendMessage(msg);
			}

		}.start();
	};

	Handler hand = new Handler() {
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			if (isFristRun()) {
				Intent intent = new Intent(StartActivity.this,
						MainActivity.class);
				startActivity(intent);
			} else {
				Intent intent = new Intent(StartActivity.this,
						FrameActivity.class);
				startActivity(intent);
			}
			finish();
		};
	};

	private boolean isFristRun() {
		SharedPreferences sharedPreferences = this.getSharedPreferences(
				"share", MODE_PRIVATE);
		boolean isFirstRun = sharedPreferences.getBoolean("isFirstRun", true);
		Editor editor = sharedPreferences.edit();
		if (!isFirstRun) {
			return false;
		} else {
			editor.putBoolean("isFirstRun", false);
			editor.commit();
			return true;
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {

		}
		return true;
	}
}



/**
 * 软件新手指引界面
 * */

public class MainActivity extends FragmentActivity {

	private ViewPager mViewPager;// 定义一个自己的viewpager

	// ViewPager 和我们的listview差不多也要一个适配器

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

	private void initView() {
		mViewPager = (ViewPager) findViewById(R.id.MyViewPager);
		ViewPagerAdapter myAdapter = new ViewPagerAdapter(
				this.getSupportFragmentManager(), MainActivity.this);
		mViewPager.setAdapter(myAdapter);
	}

	@Override
	protected void onStop() {
		finish();
		super.onStop();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {

		}
		return true;
	}
}


public class ViewPagerAdapter extends FragmentPagerAdapter{

	private Context ctx;

	//FragmentManager fragment管理器 ,上下文
	public ViewPagerAdapter(FragmentManager fm,Context ctx) {
		super(fm);
		this.ctx = ctx;
	}
	//返回一个fragment
	//arg0 滑动到第几页
	@Override
	public Fragment getItem(int arg0) {
		Fragment mFragment = null;
		if(arg0 == 0){
			mFragment = new MyFragmentone(ctx);
		}else if(arg0 == 1){
			mFragment = new MyFragmenttwo(ctx);
		}else if(arg0 == 2){
			mFragment = new MyFragmentthree(ctx);
		}
		return mFragment;
	}

	//返回适配数量
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 3;
	}

}


@SuppressLint("ValidFragment")
public class MyFragmentone extends Fragment {

	private Context ctx;// 从activity当中得到的上下文

	@SuppressLint("ValidFragment")
	public MyFragmentone(Context ctx) {
		super();
		this.ctx = ctx;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		// 初始化fragment时使用
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// fragment创建时使用
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// fragment创建view时使用
		// 返回的是一个view
		/**
		 * LayoutInflater inflater 找我们的fragmentxml时实用的 ViewGroup
		 * 使用inflater时宽高相对条件 bundler 可以通过我们的bundle在fragment创建view时传递参数
		 * */
		View view = null;
		view = View.inflate(ctx, R.layout.fragment_my01, null);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		// activity创建时使用

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// 暂停
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// activity onresume 时使用
	}

	@Override
	public void onStop() {
		// activity onStop()使用
		super.onStop();
	}

	@Override
	public void onDestroyView() {
		// fragment销毁view时进入
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		// activity销毁
		super.onDestroy();
	}

	@Override
	public void onDetach() {
		// fragment 被删除时进入
		super.onDetach();
	}
}

@SuppressLint("ValidFragment")
public class MyFragmenttwo extends Fragment {

	private Context ctx;

	public MyFragmenttwo(Context ctx) {
		super();
		this.ctx = ctx;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = null;
		view = View.inflate(ctx, R.layout.fragment_my01, null);
		LinearLayout mLinear = (LinearLayout) view
				.findViewById(R.id.Fragment01Linear);
		mLinear.setBackgroundResource(R.drawable.guidance_new2);
		return view;
	}

}


@SuppressLint("ValidFragment")
public class MyFragmentthree extends Fragment {

	private Context ctx;

	public MyFragmentthree(Context ctx) {
		super();
		this.ctx = ctx;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = null;
		view = View.inflate(ctx, R.layout.fragment_my03, null);
		ImageView mBtn = (ImageView) view.findViewById(R.id.MyLoginBtn);
		mBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent intent = new Intent(ctx, FrameActivity.class);
				ctx.startActivity(intent);
			}
		});
		return view;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值