欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ViewPager实现漂亮的引导页

程序员文章站 2023-12-13 23:23:28
废话就不多说了,直接上效果图和代码 fry.activity01 package fry; import java.util.arraylis...

废话就不多说了,直接上效果图和代码

ViewPager实现漂亮的引导页

ViewPager实现漂亮的引导页

fry.activity01

package fry;

import java.util.arraylist;
import java.util.collection;
import java.util.iterator;
import java.util.list;
import java.util.listiterator;

import com.example.viewpager.r;

import android.app.activity;
import android.os.bundle;
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.layoutparams;
import android.widget.imageview;
import android.widget.linearlayout;

public class activity01 extends activity implements onpagechangelistener,onclicklistener{
 private viewpager viewpager;
 private list<view> views;//用来存放放进viewpager里面的imageview
 private int[] imageid;//
 private imagepageradapter adapter;//适配器
 private linearlayout linearlayout;
 private int currentpoint;//当前被选中中页面的下标
 @override
 protected void oncreate(bundle savedinstancestate) {
  // todo auto-generated method stub
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity01);
  initdata();
  initview();
  
 }
 //初始化view
 void initview(){
  adapter=new imagepageradapter(views);
  viewpager=(viewpager) findviewbyid(r.id.viewpager);
  viewpager.setadapter(adapter);
  linearlayout=(linearlayout) findviewbyid(r.id.linearlayout);
  initpoint();
  viewpager.setonpagechangelistener(this);
 }
 
 //初始化数据
 void initdata(){
  //将图片的id全部放到imageid集合中
  imageid=new int[]{r.drawable.guide1,r.drawable.guide2,r.drawable.guide3,r.drawable.guide4};
  views=new arraylist<view>();
  //给iamgeview设置layoutparams参数
  layoutparams layoutparams=new layoutparams(layoutparams.match_parent, layoutparams.match_parent);
  for(int i:imageid){
   //不确定1
   imageview imageview=new imageview(this);
   imageview.setimageresource(i);
   imageview.setlayoutparams(layoutparams);
   views.add(imageview);
  }
  
  
 }
 
 /*
  * 初始化页面下方的点
  */
 private void initpoint(){
  imageview[] imageview=new imageview[imageid.length]; 
  for(int i=0;i<linearlayout.getchildcount();i++){
   imageview[i]=(imageview) linearlayout.getchildat(i);
   imageview[i].setimageresource(r.drawable.point_normal);
   imageview[i].setonclicklistener(this);
   imageview[i].settag(i);
  }
  currentpoint=0;
  imageview[currentpoint].setimageresource(r.drawable.point_select);
 }
 /*
  * 将页面下方的点全部置为未选中状态
  */
 private imageview[] clearpoint(){
  imageview[] imageview=new imageview[imageid.length]; 
  for(int i=0;i<linearlayout.getchildcount();i++){
   imageview[i]=(imageview) linearlayout.getchildat(i);
   imageview[i].setimageresource(r.drawable.point_normal);
  }
  return imageview;
 }

 @override
 public void onpagescrollstatechanged(int arg0) {
  // todo auto-generated method stub
  
 }

 @override
 public void onpagescrolled(int arg0, float arg1, int arg2) {
  // todo auto-generated method stub
  
 }

 /*
  * 页面被选中时候执行的方法
  * (non-javadoc)
  * @see android.support.v4.view.viewpager.onpagechangelistener#onpageselected(int)
  */
 @override
 public void onpageselected(int position) {
  // todo auto-generated method stub
  imageview[] imageview= clearpoint();
  imageview[position].setimageresource(r.drawable.point_select);
 }
 
 //小圆点点击事件
 @override
 public void onclick(view v) {
  // todo auto-generated method stub
  int i=(integer) v.gettag();
  viewpager.setcurrentitem(i);
 }
 
}

fry.imagepageradapter

package fry;

import java.util.arraylist;
import java.util.list;

import android.support.v4.view.pageradapter;
import android.support.v4.view.viewpager;
import android.view.view;
import android.view.viewgroup;

public class imagepageradapter extends pageradapter{

 private list<view> views;
 
 
 
 public imagepageradapter(list<view> views) {
  this.views=views;
 }

 @override
 public int getcount() {
  // todo auto-generated method stub
  return this.views.size();
 }
 /**
  * position是在viewpager中显示图片的位置
  * 我对应的图片放到对应的位置就好了
  * instantiateitem和destroyitem是对应的
  * 一个是创建item,一个是销毁item
  */
 @override
 public object instantiateitem(viewgroup container, int position) {
  ((viewpager)container).addview(views.get(position));
  return views.get(position);
 }

 @override
 public boolean isviewfromobject(view arg0, object arg1) {
  // todo auto-generated method stub
  return arg0==arg1;
 }

 @override
 public void destroyitem(viewgroup container, int position, object object) {
  ((viewpager)container).removeview(views.get(position));
 }

}

/viewpager/res/layout/activity01.xml

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

 <android.support.v4.view.viewpager
  android:id="@+id/viewpager"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >
  
 </android.support.v4.view.viewpager>
 
 <linearlayout 
  android:id="@+id/linearlayout"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_gravity="bottom|center"
  >
  <imageview 
   android:id="@+id/point1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/point_select"
   android:padding="25dip"
   />
  <imageview 
   android:id="@+id/point2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/point_normal"
   android:padding="25dip"
   />
  <imageview 
   android:id="@+id/point3"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/point_normal"
   android:padding="25dip"
   />
  <imageview 
   android:id="@+id/point4"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/point_normal"
   android:padding="25dip"
   />
  
 </linearlayout>

</framelayout>

ViewPager实现漂亮的引导页

1、多个控件(这里指小圆点)可以用list来一起访问

2、可用动态的控件设置tag来区分控件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: