当前位置:操作系统 > 安卓/Android >>

android中的左右滑屏实现By ViewPager

最近做项目要用到IPHONE 左右滑动特效,想想只能用viewpager才能实现,先看效果,就是左右滑屏的效果

 \


 

具体实现详解

android compatibility package, revision 3在7月份发布后,其中有个ViewPager引起了我的注意

官方的描述:

 请参考:http://developer.android.com/sdk/compatibility-library.html#Notes

ViewPager的下载与安装


首先通过SDK Manager更新最新版android compatibility package, revision 3

更新后,在eclipse中工程上点击右键,选择android tools -> add compatibility library即可完成安装

实际上就是一个jar包,手工导到工程中也可

jar包所在位置是\android-sdk\extras\android\compatibility\v4\android-support-v4.jar

至此准备环境已经ok
下边还是通过代码进行说话吧

准备布局文件

viewpager_layout.xml


1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3     android:layout_width="fill_parent"

4     android:layout_height="fill_parent" android:orientation="vertical">

5 <!-- 此处需要给出全路径 -->

6 <android.support.v4.view.ViewPager

7     android:id="@+id/viewpagerLayout" android:layout_height="fill_parent"android:layout_width="fill_parent"/>

8 </LinearLayout>
layout1.xml


1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3     android:layout_width="fill_parent"

4     android:layout_height="fill_parent" android:orientation="vertical">

5     <TextView android:textAppearance="?android:attr/textAppearanceLarge"android:layout_height="wrap_content" android:id="@+id/textView1"android:layout_width="fill_parent" android:text="第一页"></TextView>

6     <EditText android:layout_width="match_parent" android:layout_height="wrap_content"android:id="@+id/editText1">

7         <requestFocus></requestFocus>

8     </EditText>

9 </LinearLayout>
layout2.xml

 

1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3     android:layout_width="fill_parent"

4     android:layout_height="fill_parent" android:orientation="vertical">

5     <TextView android:textAppearance="?android:attr/textAppearanceLarge"android:layout_height="wrap_content" android:id="@+id/textView1"android:layout_width="fill_parent" android:text="第二页"></TextView>

6     <EditText android:layout_width="match_parent" android:layout_height="wrap_content"android:id="@+id/editText1">

7         <requestFocus></requestFocus>

8     </EditText>

9 </LinearLayout>
layout3.xml
1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3     android:layout_width="fill_parent"

4     android:layout_height="fill_parent" android:orientation="vertical">

5     <TextView android:textAppearance="?android:attr/textAppearanceLarge"android:layout_height="wrap_content" android:id="@+id/textView1"android:layout_width="fill_parent" android:text="第三页"></TextView>

6     <EditText android:layout_width="match_parent" android:layout_height="wrap_content"android:id="@+id/editText1">

7         <requestFocus></requestFocus>

8     </EditText>

9 </LinearLayout>
主程序
001 package a.b;

002  

003 import java.util.ArrayList;

004 import java.util.List;

005  

006 import android.app.Activity;

007 import android.os.Bundle;

008 import android.os.Parcelable;

009 import android.support.v4.view.PagerAdapter;

010 import android.support.v4.view.ViewPager;

011 import android.support.v4.view.ViewPager.OnPageChangeListener;

012 import android.util.Log;

013 import android.view.LayoutInflater;

014 import android.view.View;

015 import android.widget.EditText;

016  

017 public class TestViewPager extends Activity {

018     private ViewPager myViewPager;

019  

020     private MyPagerAdapter myAdapter;

021      

022     private LayoutInflater mInflater;

023     private List<View> mListViews;

024     private View layout1 = null;

025     private View layout2 = null;

026     private View layout3 = null;

027      

028     @Override

029     protected void onCreate(Bundle savedInstanceState) {

030         super.onCreate(savedInstanceState);

031         setContentView(R.layout.viewpager_layout);

032         myAdapter = new MyPagerAdapter();

033         myViewPager = (ViewPager) findViewById(R.id.viewpagerLayout);

034         myViewPager.setAdapter(myAdapter);

035          

036         mListViews = new ArrayList<View>();

037         mInflater = getLayoutInflater();

038         layout1 = mInflater.inflate(R.layout.layout1, null);

039         layout2 = mInflater.inflate(R.layout.layout2, null);

040         layout3 = mInflater.inflate(R.layout.layout3, null);

041         

042         mListViews.add(layout1);

043         mListViews.add(layout2);

044         mListViews.add(layout3);

045          

046         //初始化当前显示的view

047         myViewP

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,