Android编程实现的首页左右滑动切换功能示例
程序员文章站
2022-10-29 16:28:43
本文实例讲述了android编程实现的首页左右滑动切换功能。分享给大家供大家参考,具体如下:
很多软件会选择左右滑动的主界面,实现方式也很多,这里的仅供参考,勿喷。
不...
本文实例讲述了android编程实现的首页左右滑动切换功能。分享给大家供大家参考,具体如下:
很多软件会选择左右滑动的主界面,实现方式也很多,这里的仅供参考,勿喷。
不多说什么了,相信大家看看代码就明白,自己也不善言辞,望大家谅解。
自定义接口,监听滑动翻页事件:
/** 滑动后翻页事件 */ public interface onviewchangedlistener { public void onviewchanged(int viewid); }
滑动翻页view(滑动翻页不是很灵敏):
import android.content.context; import android.graphics.canvas; import android.graphics.pointf; import android.graphics.drawable.drawable; import android.util.attributeset; import android.view.motionevent; import android.view.view; import android.view.viewconfiguration; import android.widget.framelayout; import android.widget.scroller; public class scrollerview extends framelayout { private scroller scroller; private drawable drawable; private onviewchangedlistener listener; public scrollerview(context context) { this(context, null, 0); } public scrollerview(context context, attributeset attrs) { this(context, attrs, 0); } public scrollerview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); scroller = new scroller(context); } private pointf last = new pointf(); private final int touch_slop = viewconfiguration.get(getcontext()) .getscaledtouchslop(); @override public boolean onintercepttouchevent(motionevent event) { final int x = (int) event.getx(); boolean flag = false; switch (event.getaction()) { case motionevent.action_down: last.x = x; break; case motionevent.action_move: final int deltax = (int) (last.x - x); if (math.abs(deltax) > touch_slop) { flag = true; } break; case motionevent.action_up: break; } return flag; } public boolean ontouchevent(motionevent event) { final int x = (int) event.getx(); final int width = getwidth(); switch (event.getaction()) { case motionevent.action_down: if (!scroller.isfinished()) { scroller.abortanimation(); } break; case motionevent.action_move: final int deltax = (int) (last.x - x); if (math.abs(deltax) < touch_slop) { break; } last.x = x; if (deltax < 0) { if (getscrollx() > 0) { scrollby(math.max(-getscrollx(), deltax), 0); } } else if (deltax > 0) { final int availabletoscroll = getchildat(getchildcount() - 1) .getright() - getscrollx() - getwidth(); if (availabletoscroll > 0) { scrollby(math.min(availabletoscroll, deltax), 0); } } break; case motionevent.action_up: case motionevent.action_cancel: final onviewchangedlistener changedlistener = listener; int dx = (getscrollx() + width / 2) / width; if (dx < 0) { dx = 0; } if (dx > getchildcount() - 1) { dx = getchildcount() - 1; } changedlistener.onviewchanged(dx); dx *= width; dx -= getscrollx(); scroller.startscroll(getscrollx(), 0, dx, 0, math.abs(dx) * 3); break; } invalidate(); return true; } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { final int width = getwidth(); final int count = getchildcount(); int height = getheight(); int childleft = 0; for (int i = 0; i < count; i++) { final view child = getchildat(i); child.layout(childleft, 0, childleft + width, height); childleft += width; } } @override public void setbackgrounddrawable(drawable d) { super.setbackgrounddrawable(drawable); drawable = d; super.setbackgrounddrawable(null); } @override protected void dispatchdraw(canvas canvas) { if (null != drawable) { drawable.setbounds(0, 0, getchildcount() * getwidth(), getheight()); drawable.draw(canvas); } super.dispatchdraw(canvas); } @override public void computescroll() { if (scroller.computescrolloffset()) { scrollto(scroller.getcurrx(), scroller.getcurry()); invalidate(); } } /** 设置滑动后翻页事件监听 */ public void setonviewchangedlistener(onviewchangedlistener listener) { this.listener = listener; } }
主activity:
import android.app.activity; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.window; import android.widget.imageview; public class mainactivity extends activity implements onviewchangedlistener { private scrollerview container; private view view_1, view_2; private imageview imgleft, imgright; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.main); initview(); } private void initview() { layoutinflater inflater = layoutinflater.from(this); container = (scrollerview) findviewbyid(r.id.main_container); container.setonviewchangedlistener(this); imgleft = (imageview) findviewbyid(r.id.img_left); imgright = (imageview) findviewbyid(r.id.img_right); view_1 = inflater.inflate(r.layout.layout_view_1, null); view_2 = inflater.inflate(r.layout.layout_view_2, null); container.addview(view_1); container.addview(view_2); } @override public void onviewchanged(int viewid) { switch (viewid) { case 0: imgleft.setimageresource(r.drawable.main_icon_check); imgright.setimageresource(r.drawable.main_icon_normal); break; case 1: imgleft.setimageresource(r.drawable.main_icon_normal); imgright.setimageresource(r.drawable.main_icon_check); break; } } }
附:完整实例代码点击此处本站下载。
更多关于android相关内容感兴趣的读者可查看本站专题:《android布局layout技巧总结》、《android视图view技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。