ViewPager加小圆点实现第一次安装的导航

时间:2021-12-19 17:00:11

   当用户第一次安装或升级应用时,都会利用导航页面告诉用户当前版本的主要亮点,今天我们就看看这个导航的实现

    一般情况下当行页面有三部分组成,背景图片,导航文字和滑动的原点,即下面的效果

ViewPager加小圆点实现第一次安装的导航   ViewPager加小圆点实现第一次安装的导航

直接上源码

 

/** What's new 的导航界面 */
public class WhatsnewPagesA extends Activity {
/** Viewpager对象 */
private ViewPager viewPager;
private ImageView imageView;
/** 创建一个数组,用来存放每个页面要显示的View */
private ArrayList<View> pageViews;
/** 创建一个imageview类型的数组,用来表示导航小圆点 */
private ImageView[] imageViews;
/** 装显示图片的viewgroup */
private ViewGroup viewPictures;
/** 导航小圆点的viewgroup */
private ViewGroup viewPoints;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = getLayoutInflater();
pageViews = new ArrayList<View>();
pageViews.add(inflater.inflate(R.layout.viewpager01, null));
pageViews.add(inflater.inflate(R.layout.viewpager02, null));
pageViews.add(inflater.inflate(R.layout.viewpager03, null));
pageViews.add(inflater.inflate(R.layout.viewpager04, null));
pageViews.add(inflater.inflate(R.layout.viewpager05, null));
pageViews.add(inflater.inflate(R.layout.viewpager06, null));

// 小圆点数组,大小是图片的个数
imageViews = new ImageView[pageViews.size()];
// 从指定的XML文件中加载视图
viewPictures = (ViewGroup) inflater.inflate(R.layout.viewpagers, null);

viewPager = (ViewPager) viewPictures.findViewById(R.id.guidePagers);
viewPoints = (ViewGroup) viewPictures.findViewById(R.id.viewPoints);

// 添加小圆点导航的图片
for (int i = 0; i < pageViews.size(); i++) {
imageView = new ImageView(WhatsnewPagesA.this);
imageView.setLayoutParams(new LayoutParams(20, 20));
imageView.setPadding(5, 0, 5, 0);
// 吧小圆点放进数组中
imageViews[i] = imageView;
// 默认选中的是第一张图片,此时第一个小圆点是选中状态,其他不是
if (i == 0)
imageViews[i].setImageDrawable(getResources().getDrawable(
R.drawable.page_indicator_focused));
else
imageViews[i].setImageDrawable(getResources().getDrawable(
R.drawable.page_indicator_unfocused));
// 将imageviews添加到小圆点视图组
viewPoints.addView(imageViews[i]);
}

setContentView(viewPictures);

viewPager.setAdapter(new NavigationPageAdapter());
// 为viewpager添加监听,当view发生变化时的响应
viewPager.setOnPageChangeListener(new NavigationPageChangeListener());
}
}

viewpagers.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/linearLayoout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
android:id="@+id/guidePagers"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/viewPoints"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</LinearLayout>

</FrameLayout>

NavigationPageAdapter适配器

// 导航图片view的适配器,必须要实现的是下面四个方法
class NavigationPageAdapter extends PagerAdapter {

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

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}

// 初始化每个Item
@Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(pageViews.get(position));
return pageViews.get(position);
}

// 销毁每个Item
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(pageViews.get(position));
}

}

NavigationPageChangeListener监听器

// viewpager的监听器,主要是onPageSelected要实现
class NavigationPageChangeListener implements OnPageChangeListener {

@Override
public void onPageScrollStateChanged(int arg0) {

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageSelected(int position) {
// 循环主要是控制导航中每个小圆点的状态
for (int i = 0; i < imageViews.length; i++) {
// 当前view下设置小圆点为选中状态
imageViews[i].setImageDrawable(getResources().getDrawable(
R.drawable.page_indicator_focused));
// 其余设置为飞选中状态
if (position != i)
imageViews[i].setImageDrawable(getResources().getDrawable(
R.drawable.page_indicator_unfocused));
}
}

}


导航图片viewpager01.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/whatsnew_page01"
android:orientation="vertical" >

<TextView
style="@style/whatsnew_Txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/whatsnew_page01"
android:paddingTop="58dp"/>

</LinearLayout>

其他的差不多,只是背景图片又所不同
viewpager06.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/whatsnew_page01"
android:orientation="vertical" >

<TextView
style="@style/whatsnew_Txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/whatsnew_page06"
android:paddingTop="85dp" />
<Button
android:id="@+id/start_btn"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/whatsnew_start_btn"
style="@style/whatsnew_Txt"
android:layout_marginTop="105dp"
android:background="@drawable/whatsnew_start_btn"
android:onClick="startbutton"/>

</LinearLayout>



总结:

1.因为是第一次安装图片和文字等,可作为资源文件打包在项目里

2.滑动导航可以使用viewpager实现;

3.相viewpager01.xml一样,利用

LayoutInflater inflater = getLayoutInflater();

View view =inflater.inflate(R.layout.viewpager01, null);

获取view对象填充viewpager

4.原点的导航变化时机包括初始化和滑动后 先让所有的选中,不等于当前的做不选中处理

@Override
public void onPageSelected(int position) {
// 循环主要是控制导航中每个小圆点的状态
for (int i = 0; i < imageViews.length; i++) {
// 当前view下设置小圆点为选中状态
imageViews[i].setImageDrawable(getResources().getDrawable(
R.drawable.page_indicator_focused));
// 其余设置为飞选中状态
if (position != i)
imageViews[i].setImageDrawable(getResources().getDrawable(
R.drawable.page_indicator_unfocused));
}
}


 

 

 此demo我们需重点关注小圆点的生成和动作变化,下面的布局也可生成目标布局


 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >

<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagertitle"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:background="#0000"
android:textColor="#ffff" />
</android.support.v4.view.ViewPager>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal" >

<ImageView
android:id="@+id/page0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="30dp"
android:scaleType="matrix"
android:src="@drawable/page_indicator_focused" />
<ImageView
android:id="@+id/page1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="30dp"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/page_indicator_unfocused" />
<ImageView
android:id="@+id/page2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="30dp"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/page_indicator_unfocused" />
<ImageView
android:id="@+id/page3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="30dp"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/page_indicator_unfocused" />
<ImageView
android:id="@+id/page4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="30dp"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/page_indicator_unfocused" />
<ImageView
android:id="@+id/page5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="30dp"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/page_indicator_unfocused" />


</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:text="直接开始我的微信生活"
android:textSize="10sp"
android:textColor="#fff"
android:background="#888888"
android:onClick="startbutton"
/>
</LinearLayout>

</FrameLayout>

demo下载页面http://download.csdn.net/detail/candicelijx/7611829