Android ViewPager使用小结

Viewpager在项目中随处可见,通常用来做引导页、图片轮翻,或结合Fragment搭建Tab+Fragment+ViewPager项目整体框架。简单总结了一下ViewPager的常见使用方式,由于比较简单直接上代码!

1、引导页实现

引导页一般分为两种,左右滑动引导,上下滑动引导,通过ViewPager轻松实现左右滑动的引导页,并且实现引导点跟随滑动而移动的引导页。
效果图:
这里写图片描述

activity_main.xml

<RelativeLayout 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" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

    <FrameLayout
        android:id="@+id/layout_frame"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true" >

        <LinearLayout
            android:id="@+id/layout_point"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >
        </LinearLayout>
    </FrameLayout>

    <Button
        android:id="@+id/btn_go"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_above="@id/layout_frame"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:background="@drawable/shape_white_blue_4"
        android:padding="5dp"
        android:text="立即进入"
        android:textColor="@android:color/white"
        android:textSize="14sp"
        android:visibility="gone" />

</RelativeLayout>

引入v4包中的ViewPager,添加引导点的布局,其中FrameLayout包裹LinearLayout是因为,选中点(即亮点)需要跟随移动,当某个Pager被选中时选中点将该Pager之前的未选中点(即暗点)进行了遮挡,所以需要上下层关系,在布局中体现包裹关系。
MainActivity.java

/**
 * Created by magic on 2016年9月28日.引导点跟随移动的引导页
 */
public class MainActivity extends Activity implements OnPageChangeListener {
   
   

	ViewPager viewPager;
	FrameLayout layout_frame;
	LinearLayout layout_point;
	// 带颜色的引导点
	ImageView img_colorPoint;
	Button btn_go;

	List<View> list_view = new ArrayList<View>();
	List<ImageView> list_pointView = new ArrayList<ImageView>();

	//ViewPager 适配器
	ViewPagerAdapter adapter;

	// 两点之间间距
	int pointSpacing;

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

	/**
	 * 初始化引导页相关
	 */
	private void initGuideView() {
   
   
		//添加展示的View
		LayoutInflater inflater = LayoutInflater.from(this);
		list_view.add(inflater.inflate(R.layout.guide_one, null));
		list_view.add(inflater.inflate(R.layout.guide_two, null));
		list_view.add(inflater.inflate(R.layout.guide_three, null));
		adapter = new ViewPagerAdapter(list_view);
		viewPager.setAdapter(adapter);

		//添加引导点
		for (int i = 0; i < list_view.size(); i++) {
   
   
			ImageView point = new ImageView(this);
			//设置暗点
			point.setBackgroundResource(R.drawable.img_guide_point);
			LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			lp.setMargins(10, 0, 10, 0);
			point.setLayoutParams(lp);
			list_pointView.add(point);
			layout_point.addView(point);
		}

		//添加选中的引导点
		img_colorPoint = new ImageView(MainActivity.this);
		//设置亮点
		img_colorPoint.setBackgroundResource(R.drawable.img_guide_point_selected);
		LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		img_colorPoint.setLayoutParams(lp);
		layout_frame.addView(img_colorPoint);
		layout_frame.post(new Runnable() {
   
   

			@Override
			public void run() {
   
   
				//待布局绘制完毕  设置选中白点 的初始化位置
				FrameLayout.LayoutParams l = (FrameLayout.LayoutParams) img_colorPoint.getLayoutParams();
				l.leftMargin = list_pointView.get(0).getLeft();
				img_colorPoint.setLayoutParams(l);
			}
		});

		layout_point.post(new Runnable() {
   
   

			@Override
			public void run() {
   
   
				// 获取引导的之间的间隔
				pointSpacing = layout_point.getChildAt(1).getLeft()- layout_point.getChildAt(0).getLeft();
			}
		});

	}

	private void initView() {
   
   
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		layout_frame = (FrameLayout) findViewById(R.id.layout_frame);
		layout_point = (LinearLayout) findViewById(R.id.layout_point);
		btn_go = (Button) findViewById(R.id.btn_go);
		viewPager.setOnPageChangeListener(this);
		
		btn_go.setOnClickListener(new View.OnClickListener() {
   
   
			
			@Override
			public void onClick(View v) {
   
   
				
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值