網站首頁 編程語言 正文
本文實例為大家分享了Android ViewPager實現頁面左右切換的具體代碼,供大家參考,具體內容如下
主界面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實現左右滑動指引效果
?* @Description: Android實現左右滑動指引效果
?* @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;
?? ? // 包裹小圓點的LinearLayout
?? ? private ViewGroup group;
?? ? ? ?
? ? /** Called when the activity is first created. */
? ? @Override
? ? public void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? // 設置無標題窗口
? ? ? ? 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) { ?
? ? ? ? ? ? ? ? //默認選中第一張圖片
? ? ? ? ? ? ? ? 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()); ?
? ? }
? ??
? ? // 指引頁面數據適配器
? ? 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) { ?
??
? ? ? ? } ?
? ? }?
? ??
? ? // 指引頁面更改事件監聽器
? ? 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圖片為:,即當選中單個頁面時,下標變紅。
page_iindicator圖片為:,當未選中頁面時,未選中的頁面全部為黃色。
原文鏈接:https://blog.csdn.net/zhu_hua_jie/article/details/10151571
相關推薦
- 2022-12-28 Android?ViewPager2?+?Fragment?聯動效果的實現思路_Android
- 2021-12-09 Typora自動編號的具體操作_其它綜合
- 2022-08-06 C#后臺調用WebApi接口的實現方法_C#教程
- 2022-05-11 使用postman訪問k8s api
- 2022-06-19 WPF實現流光動畫特效_實用技巧
- 2023-01-03 C++定義和初始化string對象實例詳解_C 語言
- 2022-04-18 uniapp 獲取元素距離頂部的距離,實現頁面滾動元素消失后懸浮在底部的效果
- 2023-05-21 Django項目搭建之實現簡單的API訪問_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支