本文主要是介绍督促学习——ViewPager实现滑动引导界面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、
配置文件:activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><android.support.v4.view.ViewPagerandroid:id="@+id/myviewpager"android:layout_width="fill_parent"android:layout_height="wrap_content" /><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><LinearLayoutandroid:id="@+id/mybottomviewgroup"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_marginBottom="42dp"android:gravity="center_horizontal"android:orientation="horizontal" ></LinearLayout></RelativeLayout></FrameLayout>
view_01.xml/view_02.xml等:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><ImageViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:scaleType="fitXY"android:src="@drawable/pic_1" ></ImageView></LinearLayout>
MainActivity.java:
package com.ttdevs.guidedemo;public class MainActivity extends Activity {private View mView;private ArrayList<View> mPageViews;private ImageView mImageView;private ImageView[] mImageViews;private LayoutInflater mInflater;private ViewPager mViewPager;private LinearLayout mLinearLayout;private Button comeOn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 设置窗口无标题// requestWindowFeature(Window.FEATURE_NO_TITLE);mInflater = getLayoutInflater();mPageViews = new ArrayList<View>();mPageViews.add(mInflater.inflate(R.layout.view_01, null));mPageViews.add(mInflater.inflate(R.layout.view_02, null));mPageViews.add(mInflater.inflate(R.layout.view_03, null));mPageViews.add(mInflater.inflate(R.layout.view_04, null));comeOn = (Button) mPageViews.get(mPageViews.size() - 1).findViewById(R.id.comeon);mImageViews = new ImageView[mPageViews.size()];mView = mInflater.inflate(R.layout.activity_main, null);mViewPager = (ViewPager) mView.findViewById(R.id.myviewpager);mLinearLayout = (LinearLayout) mView.findViewById(R.id.mybottomviewgroup);for (int i = 0; i < mImageViews.length; i++) {mImageView = new ImageView(MainActivity.this);mImageView.setLayoutParams(new LayoutParams(10, 10));LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);lp.setMargins(0, 0, 0, 0);mImageView.setLayoutParams(lp);if (i == 0) {mImageView.setBackgroundResource(R.drawable.page_indicator_focused);} else {mImageView.setBackgroundResource(R.drawable.page_indicator_unfocused);}mImageViews[i] = mImageView;// 把指示作用的圆点图片加入底部的视图中mLinearLayout.addView(mImageView);}setContentView(mView);mViewPager.setAdapter(new MyPagerAdapter());mViewPager.setOnPageChangeListener(new OnPageChangeListener() {public void onPageSelected(int arg0) {for (int i = 0; i < mImageViews.length; i++) {if (i == arg0) {mImageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);} else {mImageViews[i].setBackgroundResource(R.drawable.page_indicator_unfocused);}}}@Overridepublic void onPageScrollStateChanged(int arg0) {}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}});comeOn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getApplicationContext(), "You can start from here,Come on ~~",Toast.LENGTH_LONG).show();}});}class MyPagerAdapter extends PagerAdapter {@Overridepublic int getCount() {return mPageViews.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}@Overridepublic int getItemPosition(Object object) {return super.getItemPosition(object);}@Overridepublic void destroyItem(View arg0, int arg1, Object arg2) {((ViewPager) arg0).removeView(mPageViews.get(arg1));}@Overridepublic Object instantiateItem(View arg0, int arg1) {((ViewPager) arg0).addView(mPageViews.get(arg1));return mPageViews.get(arg1);}}
}
2、效果图:
http://download.csdn.net/detail/ttdevs/5148757
这篇关于督促学习——ViewPager实现滑动引导界面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!