網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(shí)例為大家分享了Android ViewPager實(shí)現(xiàn)頁(yè)面左右切換的具體代碼,供大家參考,具體內(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的類(lèi):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實(shí)現(xiàn)左右滑動(dòng)指引效果
?* @Description: Android實(shí)現(xiàn)左右滑動(dòng)指引效果
?* @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;?
?? ? // 包裹滑動(dòng)圖片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è)置無(wú)標(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()); ?
? ? }
? ??
? ? // 指引頁(yè)面數(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) { ?
??
? ? ? ? } ?
? ? }?
? ??
? ? // 指引頁(yè)面更改事件監(jiān)聽(tīng)器
? ? 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)選中單個(gè)頁(yè)面時(shí),下標(biāo)變紅。
page_iindicator圖片為:,當(dāng)未選中頁(yè)面時(shí),未選中的頁(yè)面全部為黃色。
原文鏈接:https://blog.csdn.net/zhu_hua_jie/article/details/10151571
相關(guān)推薦
- 2023-07-26 TypeScript中的泛型(泛型函數(shù)、接口、類(lèi)、泛型約束)
- 2022-06-01 python機(jī)器學(xué)習(xí)sklearn實(shí)現(xiàn)識(shí)別數(shù)字_python
- 2022-10-31 Android虛擬機(jī)與類(lèi)加載機(jī)制詳情_(kāi)Android
- 2022-12-23 C++類(lèi)中如何使用定義的類(lèi)型別名_C 語(yǔ)言
- 2023-04-07 關(guān)于vector的常見(jiàn)用法詳解_C 語(yǔ)言
- 2023-02-09 利用C++開(kāi)發(fā)一個(gè)protobuf動(dòng)態(tài)解析工具_(dá)C 語(yǔ)言
- 2022-09-03 列表頁(yè)常見(jiàn)hook封裝實(shí)例_React
- 2023-02-05 Python?面向?qū)ο缶幊淘斀鈅python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支