Android开发之使用ViewPager实现导航页面(代码实现)
程序员文章站
2022-05-01 17:43:26
pageradapter类
public class guidepageadapter extends pageradapter {
private list...
pageradapter类
public class guidepageadapter extends pageradapter { private list<view> views; public guidepageadapter(list<view> views) { this.views = views; } @override public int getcount() { if (views != null) { return views.size(); } return 0; } @override public boolean isviewfromobject(view arg0, object arg1) { return (arg0 == arg1); } @override public void destroyitem(view view, int arg1, object object) { ((viewpager) view).removeview(views.get(arg1)); } @override public object instantiateitem(final view view, final int current) { ((viewpager) view).addview(views.get(current)); return views.get(current); } }
导航类layout
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" > <android.support.v4.view.viewpager android:id="@+id/vp_guide" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:id="@+id/myposition" android:layout_width="40dp" android:layout_height="20dp" android:layout_margintop="10dp" android:background="@drawable/onetebg" android:textcolor="@color/titleblue" android:gravity="center" android:layout_alignparentright="true" android:text="1"/> </relativelayout>
onetebg背景样式
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android"> <!-- 设置透明背景色 --> <solid android:color="@color/white" /> <!-- 设置一个黑色边框 --> <stroke android:width="1px" android:color="@color/transparent" /> <!-- 设置四个圆角的半径 --> <corners android:radius="14dp"/> <!-- 设置一下边距,让空间大一点 --> <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp" /> </shape>
导航类activity
public class viewpageractivity extends activity { private viewpager vp; private textview textview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.guide_activity); initviews(); initdata(); } public void initviews(){ vp = (viewpager) findviewbyid(r.id.vp_guide); textview =(textview)findviewbyid(r.id.myposition); } public void initdata(){ list<view> guidelist = new arraylist<>(); guidelist.add(getguideview(r.drawable.guide_1)); guidelist.add(getguideview(r.drawable.guide_2)); guidelist.add(getguideview(r.drawable.guide_3)); relativelayout rl = new relativelayout(this); // rl.setlayoutparams(new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent)); // rl.setbackgroundcolor(this.getresources().getcolor(r.color.transparent)); // guidelist.add(rl); log.i("lgq","sss==="+myapplication.getnscreenwidth()+"..."+viewgroup.layoutparams.match_parent); guidepageadapter vpadapter = new guidepageadapter(guidelist); vp.setadapter(vpadapter); vp.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } @override public void onpageselected(int position) { textview.settext(position+1+"/"+3); } @override public void onpagescrollstatechanged(int state) { } }); } private view getguideview(int ndrawableid) { imageview iv = new imageview(this); viewgroup.layoutparams lp = new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent); iv.setlayoutparams(lp); iv.setbackground(this.getresources().getdrawable(r.drawable.guide_1)); iv.setscaletype(imageview.scaletype.center_inside); iv.setimageresource(ndrawableid); return iv; } }
activity配置属性
<activity android:name=".viewpageractivity" android:configchanges="orientation|keyboardhidden" android:label="@string/app_name" android:screenorientation="portrait" android:windowsoftinputmode="statehidden|adjustpan"></activity>
上一篇: Java网络通信之HttpClient
推荐阅读
-
Android使用ViewPager实现导航
-
Android开发之使用150行代码实现滑动返回效果
-
Android开发之TextView使用intent传递信息,实现注册界面功能示例
-
Android开发之使用ViewPager实现导航页面(代码实现)
-
Android如何使用ViewPager2实现页面滑动切换效果
-
Android开发学习之Activities页面间的跳转实现
-
Android使用ViewPager用代码实现图片轮播效果
-
Android开发之自定义刮刮卡实现代码
-
Android动画开发之淡入淡出、缩放、旋转、平移、组合动画效果代码实现
-
vue使用原生js实现滚动页面跟踪导航高亮的示例代码