使用ViewPager实现高仿launcher左右拖动效果
程序员文章站
2023-12-04 10:54:28
前面一篇高仿launcher和墨迹左右拖动效果获得了很多朋友的好评,上一篇文章主要是通过自定义viewgroup实现的,有点麻烦。今天用viewpager这个类实现了同样的...
前面一篇高仿launcher和墨迹左右拖动效果获得了很多朋友的好评,上一篇文章主要是通过自定义viewgroup实现的,有点麻烦。今天用viewpager这个类实现了同样的效果,这样代码更少,但是效果是一样的。viewpager是实现左右两个屏幕平滑地切换的一个类,它是google提供的。
使用viewpager首先需要引入android-support-v4.jar这个jar包。具体viewpager的用法,这里不做介绍,自己从网上搜索吧!
下面先看一下效果:
<?xmlversion="1.0"encoding="utf-8"?>
<framelayoutxmlns: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>
接下来为每一个切换界面设置布局item1.xml
<?xmlversion="1.0"encoding="utf-8"?>
<linearlayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<imageview
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/guide01">
</imageview>
</linearlayout>
其他的几个界面布局和这个一样,就是修改下背景图片,所以不再复述,
最后是核心代码:
importjava.util.arraylist;
importandroid.app.activity;
importandroid.os.bundle;
importandroid.os.parcelable;
importandroid.support.v4.view.pageradapter;
importandroid.support.v4.view.viewpager;
importandroid.support.v4.view.viewpager.onpagechangelistener;
importandroid.view.layoutinflater;
importandroid.view.view;
importandroid.view.viewgroup;
importandroid.view.viewgroup.layoutparams;
importandroid.view.window;
importandroid.widget.imageview;
publicclassmainactivityextendsactivity{
viewpagerviewpager;
arraylist<view>list;
viewgroupmain,group;
imageviewimageview;
imageview[]imageviews;
@override
publicvoidoncreate(bundlesavedinstancestate){
super.oncreate(savedinstancestate);
this.requestwindowfeature(window.feature_no_title);
layoutinflaterinflater=getlayoutinflater();
list=newarraylist<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=newimageview[list.size()];
viewgroupmain=(viewgroup)inflater.inflate(r.layout.main,null);
//group是r.layou.main中的负责包裹小圆点的linearlayout.
viewgroupgroup=(viewgroup)main.findviewbyid(r.id.viewgroup);
viewpager=(viewpager)main.findviewbyid(r.id.viewpager);
for(inti=0;i<list.size();i++){
imageview=newimageview(mainactivity.this);
imageview.setlayoutparams(newlayoutparams(10,10));
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(newmyadapter());
viewpager.setonpagechangelistener(newmylistener());
}
classmyadapterextendspageradapter{
@override
publicintgetcount(){
returnlist.size();
}
@override
publicbooleanisviewfromobject(viewarg0,objectarg1){
returnarg0==arg1;
}
@override
publicintgetitemposition(objectobject){
//todoauto-generatedmethodstub
returnsuper.getitemposition(object);
}
@override
publicvoiddestroyitem(viewarg0,intarg1,objectarg2){
//todoauto-generatedmethodstub
((viewpager)arg0).removeview(list.get(arg1));
}
@override
publicobjectinstantiateitem(viewarg0,intarg1){
//todoauto-generatedmethodstub
((viewpager)arg0).addview(list.get(arg1));
returnlist.get(arg1);
}
@override
publicvoidrestorestate(parcelablearg0,classloaderarg1){
//todoauto-generatedmethodstub
}
@override
publicparcelablesavestate(){
//todoauto-generatedmethodstub
returnnull;
}
@override
publicvoidstartupdate(viewarg0){
//todoauto-generatedmethodstub
}
@override
publicvoidfinishupdate(viewarg0){
//todoauto-generatedmethodstub
}
}
classmylistenerimplementsonpagechangelistener{
@override
publicvoidonpagescrollstatechanged(intarg0){
//todoauto-generatedmethodstub
}
@override
publicvoidonpagescrolled(intarg0,floatarg1,intarg2){
//todoauto-generatedmethodstub
}
@override
publicvoidonpageselected(intarg0){
for(inti=0;i<imageviews.length;i++){
imageviews[arg0]
.setbackgroundresource(r.drawable.guide_dot_white);
if(arg0!=i){
imageviews[i]
.setbackgroundresource(r.drawable.guide_dot_black);
}
}
}
}
}
最后在提醒一句,不要忘记加入android-support-v4.jar这个jar包。
使用viewpager首先需要引入android-support-v4.jar这个jar包。具体viewpager的用法,这里不做介绍,自己从网上搜索吧!
下面先看一下效果:
效果请自行体验和上一篇比较。下面上代码:
首先是layout下面的main.xml
复制代码 代码如下:
<?xmlversion="1.0"encoding="utf-8"?>
<framelayoutxmlns: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>
接下来为每一个切换界面设置布局item1.xml
复制代码 代码如下:
<?xmlversion="1.0"encoding="utf-8"?>
<linearlayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<imageview
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/guide01">
</imageview>
</linearlayout>
其他的几个界面布局和这个一样,就是修改下背景图片,所以不再复述,
最后是核心代码:
复制代码 代码如下:
importjava.util.arraylist;
importandroid.app.activity;
importandroid.os.bundle;
importandroid.os.parcelable;
importandroid.support.v4.view.pageradapter;
importandroid.support.v4.view.viewpager;
importandroid.support.v4.view.viewpager.onpagechangelistener;
importandroid.view.layoutinflater;
importandroid.view.view;
importandroid.view.viewgroup;
importandroid.view.viewgroup.layoutparams;
importandroid.view.window;
importandroid.widget.imageview;
publicclassmainactivityextendsactivity{
viewpagerviewpager;
arraylist<view>list;
viewgroupmain,group;
imageviewimageview;
imageview[]imageviews;
@override
publicvoidoncreate(bundlesavedinstancestate){
super.oncreate(savedinstancestate);
this.requestwindowfeature(window.feature_no_title);
layoutinflaterinflater=getlayoutinflater();
list=newarraylist<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=newimageview[list.size()];
viewgroupmain=(viewgroup)inflater.inflate(r.layout.main,null);
//group是r.layou.main中的负责包裹小圆点的linearlayout.
viewgroupgroup=(viewgroup)main.findviewbyid(r.id.viewgroup);
viewpager=(viewpager)main.findviewbyid(r.id.viewpager);
for(inti=0;i<list.size();i++){
imageview=newimageview(mainactivity.this);
imageview.setlayoutparams(newlayoutparams(10,10));
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(newmyadapter());
viewpager.setonpagechangelistener(newmylistener());
}
classmyadapterextendspageradapter{
@override
publicintgetcount(){
returnlist.size();
}
@override
publicbooleanisviewfromobject(viewarg0,objectarg1){
returnarg0==arg1;
}
@override
publicintgetitemposition(objectobject){
//todoauto-generatedmethodstub
returnsuper.getitemposition(object);
}
@override
publicvoiddestroyitem(viewarg0,intarg1,objectarg2){
//todoauto-generatedmethodstub
((viewpager)arg0).removeview(list.get(arg1));
}
@override
publicobjectinstantiateitem(viewarg0,intarg1){
//todoauto-generatedmethodstub
((viewpager)arg0).addview(list.get(arg1));
returnlist.get(arg1);
}
@override
publicvoidrestorestate(parcelablearg0,classloaderarg1){
//todoauto-generatedmethodstub
}
@override
publicparcelablesavestate(){
//todoauto-generatedmethodstub
returnnull;
}
@override
publicvoidstartupdate(viewarg0){
//todoauto-generatedmethodstub
}
@override
publicvoidfinishupdate(viewarg0){
//todoauto-generatedmethodstub
}
}
classmylistenerimplementsonpagechangelistener{
@override
publicvoidonpagescrollstatechanged(intarg0){
//todoauto-generatedmethodstub
}
@override
publicvoidonpagescrolled(intarg0,floatarg1,intarg2){
//todoauto-generatedmethodstub
}
@override
publicvoidonpageselected(intarg0){
for(inti=0;i<imageviews.length;i++){
imageviews[arg0]
.setbackgroundresource(r.drawable.guide_dot_white);
if(arg0!=i){
imageviews[i]
.setbackgroundresource(r.drawable.guide_dot_black);
}
}
}
}
}
最后在提醒一句,不要忘记加入android-support-v4.jar这个jar包。