前面一篇文章實現了使用ViewPager實現高仿launcher拖動效果 ,後來很多朋友問能不能實現左右循環滑動效果和引導頁面。今天實現了左右滑動,至於在最後一頁滑動跳轉,這個也做了但是效果不是太好,也希望有實現的朋友能夠分享下。在最後一頁添加一張圖片單擊跳轉,這個認為很簡單大家自己添加個圖片,點擊後跳轉就OK。
這篇是在實現了使用ViewPager實現高仿launcher拖動效果的基礎上做了一些小的修改,可以參照前面的。廢話不多說了,直接上代碼吧!
首先看一些layout下的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" >
-
- <android.support.v4.view.ViewPager
- android:id="@+id/viewPager"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
-
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
-
- <LinearLayout
- android:id="@+id/viewGroup"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_marginBottom="30dp"
- android:gravity="center_horizontal"
- android:orientation="horizontal" >
- </LinearLayout>
- </RelativeLayout>
-
- </FrameLayout>
和前面一頁使用viewpager。使用ViewPager首先需要引入android-support-v4.jar這個jar包。自己不要忘記加。
下面是核心代碼:
- package cn.com.karl.viewpager;
-
- import java.util.ArrayList;
-
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Parcelable;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ViewGroup.LayoutParams;
- import android.view.Window;
- import android.widget.ImageView;
-
- public class MainActivity extends Activity {
- ViewPager viewPager;
- ArrayList<View> list;
- ViewGroup main, group;
- ImageView imageView;
- ImageView[] imageViews;
- private static int c_id = 0;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- LayoutInflater inflater = getLayoutInflater();
- list = new ArrayList<View>();
- list.add(inflater.inflate(R.layout.item1, null));
- list.add(inflater.inflate(R.layout.item2, null));
- list.add(inflater.inflate(R.layout.item3, null));
- list.add(inflater.inflate(R.layout.item4, null));
- list.add(inflater.inflate(R.layout.item5, null));
-
- imageViews = new ImageView[list.size()];
- ViewGroup main = (ViewGroup) inflater.inflate(R.layout.main, null);
-
- ViewGroup group = (ViewGroup) main.findViewById(R.id.viewGroup);
-
- viewPager = (ViewPager) main.findViewById(R.id.viewPager);
-
- for (int i = 0; i < list.size(); i++) {
- imageView = new ImageView(MainActivity.this);
- imageView.setLayoutParams(new LayoutParams(12,12));
- //imageView.setPadding(10, 0, 10, 0);
- imageViews[i] = imageView;
- if (i == 0) {
-
- imageViews[i].setBackgroundResource(R.drawable.guide_dot_white);
- } else {
- imageViews[i].setBackgroundResource(R.drawable.guide_dot_black);
- }
- group.addView(imageView);
- }
-
- setContentView(main);
-
- viewPager.setAdapter(new MyAdapter());
- viewPager.setOnPageChangeListener(new MyListener());
- viewPager.setCurrentItem(300);
-
- }
-
- class MyAdapter extends PagerAdapter {
-
- @Override
- public int getCount() {
- return Integer.MAX_VALUE;
- }
-
- @Override
- public boolean isViewFromObject(View arg0, Object arg1) {
- return arg0 == arg1;
- }
-
- @Override
- public int getItemPosition(Object object) {
- // TODO Auto-generated method stub
- return super.getItemPosition(object);
- }
-
- @Override
- public void destroyItem(View arg0, int arg1, Object arg2) {
- // TODO Auto-generated method stub
- //((ViewPager) arg0).removeView(list.get(arg1));
- }
-
- @Override
- public Object instantiateItem(View arg0, int arg1) {
- // TODO Auto-generated method stub
- try{
- ((ViewPager) arg0).addView(list.get(arg1%list.size()),0);
- }catch (Exception e) {
- // TODO: handle exception
- }
- return list.get(arg1%list.size());
- }
-
- @Override
- public void restoreState(Parcelable arg0, ClassLoader arg1) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public Parcelable saveState() {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public void startUpdate(View arg0) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void finishUpdate(View arg0) {
- // TODO Auto-generated method stub
-
- }
- }
-
- class MyListener implements OnPageChangeListener {
-
- //當滑動狀態改變時調用
- @Override
- public void onPageScrollStateChanged(int arg0) {
- // TODO Auto-generated method stub
- //arg0=arg0%list.size();
-
- }
-
- //當當前頁面被滑動時調用
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- // TODO Auto-generated method stub
-
- }
-
- //當新的頁面被選中時調用
- @Override
- public void onPageSelected(int arg0) {
- if(arg0>2){
- arg0=arg0%list.size();
- }
- c_id = arg0;
- for (int i = 0; i < imageViews.length; i++) {
- imageViews[arg0]
- .setBackgroundResource(R.drawable.guide_dot_white);
- if (arg0 != i) {
- imageViews[i]
- .setBackgroundResource(R.drawable.guide_dot_black);
- }
- }
-
- Log.e("-------------", "當前是第"+c_id+"頁");
- }
-
- }
- }
代碼跟上一篇差不多,大家自己研究吧!下面看下效果:
忘記了,這種效果不太好用圖片演示,大家自己演示吧!滑動到最後一張,再滑動就是進入了第一頁。下面看下我logcat下的打印:
這裡默認是從第0頁開始的。
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11