Android ViewPager實現(xiàn)頁面左右切換效果
本文實例為大家分享了Android ViewPager實現(xiàn)頁面左右切換的具體代碼,供大家參考,具體內(nèi)容如下
主界面viewpager.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/linearLayout01" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="vertical" > ?? ? ? ?<include android:id="@+id/item_header" ?? ? ? ? ? ??? ?layout="@layout/item_header" /> ?? ? ? ?<android.support.v4.view.ViewPager ?? ? ? ? ? ?android:id="@+id/guidePages" ?? ? ? ? ? ?android:layout_width="fill_parent" ?? ? ? ? ? ?android:layout_height="wrap_content"/> ? ? ?? ? ?? ? ? </LinearLayout> ?? ? ?? ? ? <LinearLayout ? ? ? ? android:id="@+id/linearLayout01" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="vertical" > ? ? ? ?? ? ? <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="40dp" ? ? ? ? ? android:gravity="center_horizontal" ? ? ? ? ? android:orientation="horizontal" > ? ? ? ? ?? ? ? </LinearLayout> ? ? ?? ? ? </RelativeLayout> ? ? </LinearLayout> </FrameLayout>
處理ViewPager的類:MyGuideViewActivity.java
package com.example.wenandroid; ? import java.util.ArrayList; ? import android.app.Activity; 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.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; ? /** ?* Android實現(xiàn)左右滑動指引效果 ?* @Description: Android實現(xiàn)左右滑動指引效果 ?* @File: MyGuideViewActivity.java ?* @Package com.test.guide ?* @Author Hanyonglu ?* @Date 2012-4-6 下午11:15:18 ?* @Version V1.0 ?*/ public class MyGuideViewActivity extends Activity { ?? ? private ViewPager viewPager; ? ?? ? private ArrayList<View> pageViews; ? ?? ? private ImageView imageView; ? ?? ? private ImageView[] imageViews;? ?? ? // 包裹滑動圖片LinearLayout ?? ? private ViewGroup main; ?? ? // 包裹小圓點(diǎn)的LinearLayout ?? ? private ViewGroup group; ?? ? ? ? ? ? /** Called when the activity is first created. */ ? ? @Override ? ? public void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? // 設(shè)置無標(biāo)題窗口 ? ? ? ? requestWindowFeature(Window.FEATURE_NO_TITLE); ? ? ? ?? ? ? ? ? LayoutInflater inflater = getLayoutInflater(); ? ? ? ? ? pageViews = new ArrayList<View>(); ? ? ? ? ? pageViews.add(inflater.inflate(R.layout.item05, null)); ? ? ? ? pageViews.add(inflater.inflate(R.layout.item06, null)); ? ? ? ? pageViews.add(inflater.inflate(R.layout.item01, null)); ? ? ? ? ? pageViews.add(inflater.inflate(R.layout.item02, null)); ? ? ? ? ? pageViews.add(inflater.inflate(R.layout.item03, null)); ? ? ? ? ? pageViews.add(inflater.inflate(R.layout.item04, null)); ? ? ? ? ?? ? ? ? ? imageViews = new ImageView[pageViews.size()]; ? ? ? ? ? main = (ViewGroup)inflater.inflate(R.layout.viewpager, null); ? ? ? ? ?? ? ? ? ? group = (ViewGroup)main.findViewById(R.id.viewGroup); ? ? ? ? ? viewPager = (ViewPager)main.findViewById(R.id.guidePages); ? ? ? ? ?? ? ? ? ? for (int i = 0; i < pageViews.size(); i++) { ? ? ? ? ? ? ? imageView = new ImageView(MyGuideViewActivity.this); ? ? ? ? ? ? ? imageView.setLayoutParams(new LayoutParams(20,20)); ? ? ? ? ? ? ? imageView.setPadding(20, 0, 20, 0); ? ? ? ? ? ? ? imageViews[i] = imageView; ? ? ? ? ? ? ?? ? ? ? ? ? ? if (i == 0) { ? ? ? ? ? ? ? ? ? //默認(rèn)選中第一張圖片 ? ? ? ? ? ? ? ? imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused); ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? imageViews[i].setBackgroundResource(R.drawable.page_indicator); ? ? ? ? ? ? ? } ? ? ? ? ? ? ?? ? ? ? ? ? ? group.addView(imageViews[i]); ? ? ? ? ? } ? ? ? ? ?? ? ? ? ? setContentView(main); ? ? ? ?? ? ? ? ? viewPager.setAdapter(new GuidePageAdapter()); ? ? ? ? ? viewPager.setOnPageChangeListener(new GuidePageChangeListener()); ? ? ? } ? ?? ? ? // 指引頁面數(shù)據(jù)適配器 ? ? class GuidePageAdapter extends PagerAdapter { ? ? ?? ? ? ? ? ? ? @Override ? ? ? ? ? public int getCount() { ? ? ? ? ? ? ? return pageViews.size(); ? ? ? ? ? } ? ?? ? ? ? ? @Override ? ? ? ? ? public boolean isViewFromObject(View arg0, Object arg1) { ? ? ? ? ? ? ? return arg0 == arg1; ? ? ? ? ? } ? ?? ? ? ? ? @Override ? ? ? ? ? public int getItemPosition(Object object) { ? ? ? ? ? ? ? return super.getItemPosition(object); ? ? ? ? ? } ? ?? ? ? ? ? @Override ? ? ? ? ? public void destroyItem(View arg0, int arg1, Object arg2) { ? ? ? ? ? ? ? ((ViewPager) arg0).removeView(pageViews.get(arg1)); ? ? ? ? ? } ? ?? ? ? ? ? @Override ? ? ? ? ? public Object instantiateItem(View arg0, int arg1) { ? ? ? ? ? ? ? ((ViewPager) arg0).addView(pageViews.get(arg1)); ? ? ? ? ? ? ? return pageViews.get(arg1); ? ? ? ? ? } ? ?? ? ? ? ? @Override ? ? ? ? ? public void restoreState(Parcelable arg0, ClassLoader arg1) { ? ?? ? ? ? ? } ? ?? ? ? ? ? @Override ? ? ? ? ? public Parcelable saveState() { ? ? ? ? ? ? ? return null; ? ? ? ? ? } ? ?? ? ? ? ? @Override ? ? ? ? ? public void startUpdate(View arg0) { ? ?? ? ? ? ? } ? ?? ? ? ? ? @Override ? ? ? ? ? public void finishUpdate(View arg0) { ? ?? ? ? ? ? } ? ? ? }? ? ?? ? ? // 指引頁面更改事件監(jiān)聽器 ? ? class GuidePageChangeListener implements OnPageChangeListener { ? ? ? ?? ? ? ? ? ? ? @Override ? ? ? ? ? public void onPageScrollStateChanged(int arg0) { ? ?? ? ? ? ? } ? ?? ? ? ? ? @Override ? ? ? ? ? public void onPageScrolled(int arg0, float arg1, int arg2) { ? ?? ? ? ? ? } ? ?? ? ? ? ? @Override ? ? ? ? ? public void onPageSelected(int arg0) { ? ? ? ? ? ? ? for (int i = 0; i < imageViews.length; i++) { ? ? ? ? ? ? ? ? ? imageViews[arg0].setBackgroundResource(R.drawable.page_indicator_focused); ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? if (arg0 != i) { ? ? ? ? ? ? ? ? ? ? ? imageViews[i].setBackgroundResource(R.drawable.page_indicator); ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } ? ? ? ? } ? ? ? } ? }
其中page_indicator_focused圖片為:,即當(dāng)選中單個頁面時,下標(biāo)變紅。
page_iindicator圖片為:,當(dāng)未選中頁面時,未選中的頁面全部為黃色。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
搭建簡易藍(lán)牙定位系統(tǒng)的實現(xiàn)方法
下面小編就為大家?guī)硪黄罱ê喴姿{(lán)牙定位系統(tǒng)的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03Android開發(fā)之ViewPager實現(xiàn)滑動切換頁面
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)之ViewPager實現(xiàn)滑動切換頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-09-09深入解析Android App開發(fā)中Context的用法
這篇文章主要介紹了深入解析Android App開發(fā)中Context的用法,包括Context的創(chuàng)建場景和Context對資源的訪問等內(nèi)容,需要的朋友可以參考下2016-02-02詳細(xì)介紹Android中的視圖焦點(diǎn)Focus的使用
本篇文章主要介紹了詳細(xì)介紹Android中的視圖焦點(diǎn)Focus的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01Android使用Sensor感應(yīng)器實現(xiàn)線程中刷新UI創(chuàng)建android測力計的功能
這篇文章主要介紹了Android使用Sensor感應(yīng)器實現(xiàn)線程中刷新UI創(chuàng)建android測力計的功能,實例分析了Android使用Sensor感應(yīng)器實現(xiàn)UI刷新及創(chuàng)建測力器的技巧,需要的朋友可以參考下2015-12-12