现在的位置: 首页 > 编程语言 > 正文

ViewPager首次安装引导页(中级代码)

2018年05月16日 编程语言 ⁄ 共 5392字 ⁄ 字号 评论关闭
它下面加圆点或者文字描述,其实是ViewPager和textvIEW等等布局在一个文件里面,其它布局通过监听viewPager的变化来动态改变自己而已,就是如此,一点也不神奇。

package com.example.listviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
/**
 *http://blog.csdn.net/qinyuanpei/article/details/17186319
 *这有几篇文章
 *
 *其实,下面的几个圆点是通过我们相对布局来弄上的,与本身的ViewPager并没有太大关系。
 */
public class BannerActivity extends Activity implements OnClickListener {
	ViewPager mViewPager ;
	PagerAdapter mPagerAdapter ;
	private int[] draws = new int[]{R.drawable.bg,R.drawable.empty_paper_tape,
			R.drawable.login_warring_pic,R.drawable.share_tellfriends_bg,R.drawable.splash_bg};
	
	 //记录当前选中位置  
    private int currentIndex; 
    //底部小图片
    private ImageView[] dots ;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.banner);
		
		mViewPager = (ViewPager)findViewById(R.id.banner);
		
		initAdapter();
		
		mViewPager.setAdapter(mPagerAdapter);
		
		initListener();
		
		//初始化底部小点  
        initDots();
	}
	
	private void initDots(){
		dots = new ImageView[draws.length];
		LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
		for (int i = 0; i < draws.length; i++) {
			dots[i] = (ImageView)ll.getChildAt(i);
			dots[i].setEnabled(true);
			dots[i].setOnClickListener(this);
			dots[i].setTag(i);//设置位置tag,方便取出与当前位置对应  
		}
		
		currentIndex = 0;
		dots[currentIndex].setEnabled(false);
	}
	
	private void initAdapter(){
		mPagerAdapter = new PagerAdapter() {
			
			@Override
			public Object instantiateItem(ViewGroup container, int position) {
				ImageView imageView = new ImageView(BannerActivity.this);
				imageView.setScaleType(ScaleType.FIT_XY);
				imageView.setImageResource(draws[position]);
				container.addView(imageView);
				return imageView;
			}
			
			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				ImageView imageView = new ImageView(BannerActivity.this);
				imageView.setScaleType(ScaleType.FIT_XY);
				imageView.setImageResource(draws[position]);
				container.removeView(imageView);
			}
			
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == (View)arg1;
			}
			
			@Override
			public int getCount() {
				return draws == null ? 0 : draws.length;//因为会有一个左右尽头的数据
			}
		};
	}
	
	private void initListener(){
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int pos) {
				setCurDot(pos);
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
	}
	
	private void setCurDot(int positon){
		 if (positon < 0 || positon > draws.length - 1 || currentIndex == positon) {  
	            return;  
	     } 
		 dots[positon] .setEnabled(false);
		 dots[currentIndex].setEnabled(true);
		 currentIndex = positon;
	}
	
	private void setCurView(int position){
		if (position < 0 || position >= draws.length) {  
            return;  
        }  
		mViewPager.setCurrentItem(position);
	}

	@Override
	public void onClick(View v) {
		int pos = (Integer)v.getTag();
		setCurDot(pos);
		setCurView(pos);
	}

}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    
  <android.support.v4.view.ViewPager
      android:id="@+id/banner"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fadingEdge="none"/>
  
  
  <LinearLayout   
        android:id="@+id/ll"   
        android:orientation="horizontal"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:layout_marginBottom="24.0dip"   
        android:layout_alignParentBottom="true"   
        android:layout_centerHorizontal="true">  
          
        <ImageView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center_vertical"  
            android:clickable="true"  
            android:padding="15.0dip"  
            android:src="@drawable/dot" />  
  
        <ImageView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center_vertical"  
            android:clickable="true"  
            android:padding="15.0dip"  
            android:src="@drawable/dot" />  
  
        <ImageView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center_vertical"  
            android:clickable="true"  
            android:padding="15.0dip"  
            android:src="@drawable/dot" />  
  
        <ImageView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center_vertical"  
            android:clickable="true"  
            android:padding="15.0dip"  
            android:src="@drawable/dot" />  
        <ImageView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center_vertical"  
            android:clickable="true"  
            android:padding="15.0dip"  
            android:src="@drawable/dot" />  
  
    </LinearLayout>  
  
</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio="3"
    android:shape="ring"
    android:thickness="10dp"
    android:useLevel="false" >

    <solid android:color="#FF0000" />

    <size
        android:height="10dp"
        android:width="10dp" />

</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio="3"
    android:shape="ring"
    android:thickness="10dp"
    android:useLevel="false" >

    <solid android:color="#0000FF" />

    <size
        android:height="10dp"
        android:width="10dp" />

</shape>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_enabled="true" android:drawable="@drawable/circlewhite"></item>
    <item android:state_enabled="false" android:drawable="@drawable/circleblack"></item>

</selector>



抱歉!评论已关闭.