Android开发中滑动分页功能实例详解
程序员文章站
2023-12-14 16:47:52
本文实例讲述了android开发中滑动分页功能。分享给大家供大家参考,具体如下:
android ui 往右滑动,滑动到最后一页就自动加载数据并显示
如图:
ja...
本文实例讲述了android开发中滑动分页功能。分享给大家供大家参考,具体如下:
android ui 往右滑动,滑动到最后一页就自动加载数据并显示
如图:
java代码:
package cn.anycall.ju; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import android.app.activity; import android.content.activitynotfoundexception; import android.content.context; import android.content.intent; import android.content.pm.resolveinfo; import android.os.bundle; import android.os.handler; import android.os.looper; import android.os.message; import android.view.keyevent; import android.view.view; import android.view.window; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.gridview; import android.widget.toast; import cn.anycall.ju.scrolllayout.onscreenchangelistenerdataload; /** * gridview分页显示安装的应用程序 */ public class allapplist extends activity { private scrolllayout mscrolllayout; private static final float app_page_size = 4.0f; private context mcontext; private pagecontrolview pagecontrol; public myhandler myhandler; public int n=0; private dataloading dataload; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); this.requestwindowfeature(window.feature_no_title); mcontext = this; setcontentview(r.layout.main); dataload = new dataloading(); mscrolllayout = (scrolllayout)findviewbyid(r.id.scrolllayouttest); myhandler = new myhandler(this,1); //起一个线程更新数据 mythread m = new mythread(); new thread(m).start(); } /** * gridview 的onitemlick响应事件 */ public onitemclicklistener listener = new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub system.out.println("position="+position); } }; @override protected void ondestroy() { // todo auto-generated method stub android.os.process.killprocess(android.os.process.mypid()); super.ondestroy(); } @override public boolean onkeydown(int keycode, keyevent event) { // todo auto-generated method stub if (keycode == keyevent.keycode_back) { finish(); return true; } return super.onkeydown(keycode, event); } // 更新后台数据 class mythread implements runnable { public void run() { try { thread.sleep(1000*3); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } string msglist = "1"; message msg = new message(); bundle b = new bundle();// 存放数据 b.putstring("rmsg", msglist); msg.setdata(b); allapplist.this.myhandler.sendmessage(msg); // 向handler发送消息,更新ui } } class myhandler extends handler { private allapplist mcontext; public myhandler(context conn,int a) { mcontext = (allapplist)conn; } public myhandler(looper l) { super(l); } // 子类必须重写此方法,接受数据 @override public void handlemessage(message msg) { // todo auto-generated method stub super.handlemessage(msg); bundle b = msg.getdata(); string rmsg = b.getstring("rmsg"); if ("1".equals(rmsg)) { // do nothing list<map> list = new arraylist<map>(); for(int i =0;i<16;i++){ n++; map map = new hashmap(); map.put("name", n); list.add(map); } int pageno = (int)math.ceil( list.size()/app_page_size); for (int i = 0; i < pageno; i++) { gridview apppage = new gridview(mcontext); // get the "i" page data apppage.setadapter(new appadapter(mcontext, list, i)); apppage.setnumcolumns(2); apppage.setonitemclicklistener(listener); mscrolllayout.addview(apppage); } //加载分页 pagecontrol = (pagecontrolview) findviewbyid(r.id.pagecontrol); pagecontrol.bindscrollviewgroup(mscrolllayout); //加载分页数据 dataload.bindscrollviewgroup(mscrolllayout); } } } //分页数据 class dataloading { private int count; public void bindscrollviewgroup(scrolllayout scrollviewgroup) { this.count=scrollviewgroup.getchildcount(); scrollviewgroup.setonscreenchangelistenerdataload(new onscreenchangelistenerdataload() { public void onscreenchange(int currentindex) { // todo auto-generated method stub generatepagecontrol(currentindex); } }); } private void generatepagecontrol(int currentindex){ //如果到最后一页,就加载16条记录 if(count==currentindex+1){ mythread m = new mythread(); new thread(m).start(); } } } }
java代码:
package cn.anycall.ju; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import android.app.activity; import android.content.activitynotfoundexception; import android.content.context; import android.content.intent; import android.content.pm.resolveinfo; import android.os.bundle; import android.os.handler; import android.os.looper; import android.os.message; import android.view.keyevent; import android.view.view; import android.view.window; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.gridview; import android.widget.toast; import cn.anycall.ju.scrolllayout.onscreenchangelistenerdataload; /** * gridview分页显示安装的应用程序 */ public class allapplist extends activity { private scrolllayout mscrolllayout; private static final float app_page_size = 4.0f; private context mcontext; private pagecontrolview pagecontrol; public myhandler myhandler; public int n=0; private dataloading dataload; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); this.requestwindowfeature(window.feature_no_title); mcontext = this; setcontentview(r.layout.main); dataload = new dataloading(); mscrolllayout = (scrolllayout)findviewbyid(r.id.scrolllayouttest); myhandler = new myhandler(this,1); //起一个线程更新数据 mythread m = new mythread(); new thread(m).start(); } /** * gridview 的onitemlick响应事件 */ public onitemclicklistener listener = new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub system.out.println("position="+position); } }; @override protected void ondestroy() { // todo auto-generated method stub android.os.process.killprocess(android.os.process.mypid()); super.ondestroy(); } @override public boolean onkeydown(int keycode, keyevent event) { // todo auto-generated method stub if (keycode == keyevent.keycode_back) { finish(); return true; } return super.onkeydown(keycode, event); } // 更新后台数据 class mythread implements runnable { public void run() { try { thread.sleep(1000*3); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } string msglist = "1"; message msg = new message(); bundle b = new bundle();// 存放数据 b.putstring("rmsg", msglist); msg.setdata(b); allapplist.this.myhandler.sendmessage(msg); // 向handler发送消息,更新ui } } class myhandler extends handler { private allapplist mcontext; public myhandler(context conn,int a) { mcontext = (allapplist)conn; } public myhandler(looper l) { super(l); } // 子类必须重写此方法,接受数据 @override public void handlemessage(message msg) { // todo auto-generated method stub super.handlemessage(msg); bundle b = msg.getdata(); string rmsg = b.getstring("rmsg"); if ("1".equals(rmsg)) { // do nothing list<map> list = new arraylist<map>(); for(int i =0;i<16;i++){ n++; map map = new hashmap(); map.put("name", n); list.add(map); } int pageno = (int)math.ceil( list.size()/app_page_size); for (int i = 0; i < pageno; i++) { gridview apppage = new gridview(mcontext); // get the "i" page data apppage.setadapter(new appadapter(mcontext, list, i)); apppage.setnumcolumns(2); apppage.setonitemclicklistener(listener); mscrolllayout.addview(apppage); } //加载分页 pagecontrol = (pagecontrolview) findviewbyid(r.id.pagecontrol); pagecontrol.bindscrollviewgroup(mscrolllayout); //加载分页数据 dataload.bindscrollviewgroup(mscrolllayout); } } } //分页数据 class dataloading { private int count; public void bindscrollviewgroup(scrolllayout scrollviewgroup) { this.count=scrollviewgroup.getchildcount(); scrollviewgroup.setonscreenchangelistenerdataload(new onscreenchangelistenerdataload() { public void onscreenchange(int currentindex) { // todo auto-generated method stub generatepagecontrol(currentindex); } }); } private void generatepagecontrol(int currentindex){ //如果到最后一页,就加载16条记录 if(count==currentindex+1){ mythread m = new mythread(); new thread(m).start(); } } } }
java代码:
package cn.anycall.ju; import java.util.arraylist; import java.util.list; import java.util.map; import android.content.context; import android.content.pm.packagemanager; import android.content.pm.resolveinfo; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import cn.anycall.ju.r; public class appadapter extends baseadapter { private list<map> mlist; private context mcontext; public static final int app_page_size = 4; private packagemanager pm; public appadapter(context context, list<map> list, int page) { mcontext = context; pm = context.getpackagemanager(); mlist = new arraylist<map>(); int i = page * app_page_size; int iend = i+app_page_size; while ((i<list.size()) && (i<iend)) { mlist.add(list.get(i)); i++; } } public int getcount() { // todo auto-generated method stub return mlist.size(); } public object getitem(int position) { // todo auto-generated method stub return mlist.get(position); } public long getitemid(int position) { // todo auto-generated method stub return position; } public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub map appinfo = mlist.get(position); appitem appitem; if (convertview == null) { view v = layoutinflater.from(mcontext).inflate(r.layout.app_item, null); appitem = new appitem(); appitem.mappicon = (imageview)v.findviewbyid(r.id.imgdetail); appitem.mappname = (textview)v.findviewbyid(r.id.tuaninfo); v.settag(appitem); convertview = v; } else { appitem = (appitem)convertview.gettag(); } // set the icon appitem.mappicon.setimageresource(r.drawable.icon); // set the app name appitem.mappname.settext(appinfo.get("name").tostring()); return convertview; } /** * 每个应用显示的内容,包括图标和名称 * @author yao.guet * */ class appitem { imageview mappicon; textview mappname; } }
java代码:
package cn.anycall.ju; import java.util.arraylist; import java.util.list; import java.util.map; import android.content.context; import android.content.pm.packagemanager; import android.content.pm.resolveinfo; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import cn.anycall.ju.r; public class appadapter extends baseadapter { private list<map> mlist; private context mcontext; public static final int app_page_size = 4; private packagemanager pm; public appadapter(context context, list<map> list, int page) { mcontext = context; pm = context.getpackagemanager(); mlist = new arraylist<map>(); int i = page * app_page_size; int iend = i+app_page_size; while ((i<list.size()) && (i<iend)) { mlist.add(list.get(i)); i++; } } public int getcount() { // todo auto-generated method stub return mlist.size(); } public object getitem(int position) { // todo auto-generated method stub return mlist.get(position); } public long getitemid(int position) { // todo auto-generated method stub return position; } public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub map appinfo = mlist.get(position); appitem appitem; if (convertview == null) { view v = layoutinflater.from(mcontext).inflate(r.layout.app_item, null); appitem = new appitem(); appitem.mappicon = (imageview)v.findviewbyid(r.id.imgdetail); appitem.mappname = (textview)v.findviewbyid(r.id.tuaninfo); v.settag(appitem); convertview = v; } else { appitem = (appitem)convertview.gettag(); } // set the icon appitem.mappicon.setimageresource(r.drawable.icon); // set the app name appitem.mappname.settext(appinfo.get("name").tostring()); return convertview; } /** * 每个应用显示的内容,包括图标和名称 * @author yao.guet * */ class appitem { imageview mappicon; textview mappname; } }
java代码:
package cn.anycall.ju; import android.content.context; import android.util.attributeset; import android.widget.imageview; import android.widget.linearlayout; import cn.anycall.ju.r; import cn.anycall.ju.scrolllayout.onscreenchangelistener; public class pagecontrolview extends linearlayout { private context context; private int count; public void bindscrollviewgroup(scrolllayout scrollviewgroup) { this.count=scrollviewgroup.getchildcount(); system.out.println("count="+count); generatepagecontrol(scrollviewgroup.getcurrentscreenindex()); scrollviewgroup.setonscreenchangelistener(new onscreenchangelistener() { public void onscreenchange(int currentindex) { // todo auto-generated method stub generatepagecontrol(currentindex); } }); } public pagecontrolview(context context) { super(context); this.init(context); } public pagecontrolview(context context, attributeset attrs) { super(context, attrs); this.init(context); } private void init(context context) { this.context=context; } private void generatepagecontrol(int currentindex) { this.removeallviews(); int pagenum = 6; // 显示多少个 int pageno = currentindex+1; //第几页 int pagesum = this.count; //总共多少页 if(pagesum>1){ int currentnum = (pageno % pagenum == 0 ? (pageno / pagenum) - 1 : (int) (pageno / pagenum)) * pagenum; if (currentnum < 0) currentnum = 0; if (pageno > pagenum){ imageview imageview = new imageview(context); imageview.setimageresource(r.drawable.zuo); this.addview(imageview); } for (int i = 0; i < pagenum; i++) { if ((currentnum + i + 1) > pagesum || pagesum < 2) break; imageview imageview = new imageview(context); if(currentnum + i + 1 == pageno){ imageview.setimageresource(r.drawable.page_indicator_focused); }else{ imageview.setimageresource(r.drawable.page_indicator); } this.addview(imageview); } if (pagesum > (currentnum + pagenum)) { imageview imageview = new imageview(context); imageview.setimageresource(r.drawable.you); this.addview(imageview); } } } }
java代码:
package cn.anycall.ju; import android.content.context; import android.util.attributeset; import android.widget.imageview; import android.widget.linearlayout; import cn.anycall.ju.r; import cn.anycall.ju.scrolllayout.onscreenchangelistener; public class pagecontrolview extends linearlayout { private context context; private int count; public void bindscrollviewgroup(scrolllayout scrollviewgroup) { this.count=scrollviewgroup.getchildcount(); system.out.println("count="+count); generatepagecontrol(scrollviewgroup.getcurrentscreenindex()); scrollviewgroup.setonscreenchangelistener(new onscreenchangelistener() { public void onscreenchange(int currentindex) { // todo auto-generated method stub generatepagecontrol(currentindex); } }); } public pagecontrolview(context context) { super(context); this.init(context); } public pagecontrolview(context context, attributeset attrs) { super(context, attrs); this.init(context); } private void init(context context) { this.context=context; } private void generatepagecontrol(int currentindex) { this.removeallviews(); int pagenum = 6; // 显示多少个 int pageno = currentindex+1; //第几页 int pagesum = this.count; //总共多少页 if(pagesum>1){ int currentnum = (pageno % pagenum == 0 ? (pageno / pagenum) - 1 : (int) (pageno / pagenum)) * pagenum; if (currentnum < 0) currentnum = 0; if (pageno > pagenum){ imageview imageview = new imageview(context); imageview.setimageresource(r.drawable.zuo); this.addview(imageview); } for (int i = 0; i < pagenum; i++) { if ((currentnum + i + 1) > pagesum || pagesum < 2) break; imageview imageview = new imageview(context); if(currentnum + i + 1 == pageno){ imageview.setimageresource(r.drawable.page_indicator_focused); }else{ imageview.setimageresource(r.drawable.page_indicator); } this.addview(imageview); } if (pagesum > (currentnum + pagenum)) { imageview imageview = new imageview(context); imageview.setimageresource(r.drawable.you); this.addview(imageview); } } } }
java代码:
package cn.anycall.ju; import android.content.context; import android.graphics.canvas; import android.util.attributeset; import android.util.log; import android.view.motionevent; import android.view.velocitytracker; import android.view.view; import android.view.viewconfiguration; import android.view.viewgroup; import android.widget.scroller; /** * 仿launcher中的worksapce,可以左右滑动切换屏幕的类 * */ public class scrolllayout extends viewgroup { private static final string tag = "scrolllayout"; private scroller mscroller; private velocitytracker mvelocitytracker; private int mcurscreen; private int mdefaultscreen = 0; private static final int touch_state_rest = 0; private static final int touch_state_scrolling = 1; private static final int snap_velocity = 600; private int mtouchstate = touch_state_rest; private int mtouchslop; private float mlastmotionx; private float mlastmotiony; private int currentscreenindex = 0; public int getcurrentscreenindex() { return currentscreenindex; } public void setcurrentscreenindex(int currentscreenindex) { this.currentscreenindex = currentscreenindex; } public scrolllayout(context context, attributeset attrs) { this(context, attrs, 0); // todo auto-generated constructor stub } public scrolllayout(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); // todo auto-generated constructor stub mscroller = new scroller(context); mcurscreen = mdefaultscreen; mtouchslop = viewconfiguration.get(getcontext()).getscaledtouchslop(); } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { // todo auto-generated method stub int childleft = 0; final int childcount = getchildcount(); system.out.println("childcount=" + childcount); for (int i = 0; i < childcount; i++) { final view childview = getchildat(i); if (childview.getvisibility() != view.gone) { final int childwidth = childview.getmeasuredwidth(); childview.layout(childleft, 0, childleft + childwidth, childview.getmeasuredheight()); childleft += childwidth; } } } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { log.e(tag, "onmeasure"); super.onmeasure(widthmeasurespec, heightmeasurespec); final int width = measurespec.getsize(widthmeasurespec); final int widthmode = measurespec.getmode(widthmeasurespec); if (widthmode != measurespec.exactly) { throw new illegalstateexception( "scrolllayout only canmcurscreen run at exactly mode!"); } final int heightmode = measurespec.getmode(heightmeasurespec); if (heightmode != measurespec.exactly) { throw new illegalstateexception( "scrolllayout only can run at exactly mode!"); } // the children are given the same width and height as the scrolllayout final int count = getchildcount(); for (int i = 0; i < count; i++) { getchildat(i).measure(widthmeasurespec, heightmeasurespec); } system.out.println("moving to screen " + mcurscreen); scrollto(mcurscreen * width, 0); } /** * according to the position of current layout scroll to the destination * page. */ public void snaptodestination() { final int screenwidth = getwidth(); final int destscreen = (getscrollx() + screenwidth / 2) / screenwidth; snaptoscreen(destscreen); } public void snaptoscreen(int whichscreen) { // get the valid layout page whichscreen = math.max(0, math.min(whichscreen, getchildcount() - 1)); if (getscrollx() != (whichscreen * getwidth())) { final int delta = whichscreen * getwidth() - getscrollx(); mscroller.startscroll(getscrollx(), 0, delta, 0, math.abs(delta) * 2); mcurscreen = whichscreen; invalidate(); // redraw the layout } } public void settoscreen(int whichscreen) { whichscreen = math.max(0, math.min(whichscreen, getchildcount() - 1)); mcurscreen = whichscreen; scrollto(whichscreen * getwidth(), 0); } public int getcurscreen() { return mcurscreen; } @override public void computescroll() { // todo auto-generated method stub if (mscroller.computescrolloffset()) { scrollto(mscroller.getcurrx(), mscroller.getcurry()); postinvalidate(); } } @override public boolean ontouchevent(motionevent event) { // todo auto-generated method stub if (mvelocitytracker == null) { mvelocitytracker = velocitytracker.obtain(); } mvelocitytracker.addmovement(event); final int action = event.getaction(); final float x = event.getx(); final float y = event.gety(); switch (action) { case motionevent.action_down: log.e(tag, "event down!"); if (!mscroller.isfinished()) { mscroller.abortanimation(); } mlastmotionx = x; break; case motionevent.action_move: int deltax = (int) (mlastmotionx - x); mlastmotionx = x; scrollby(deltax, 0); break; case motionevent.action_up: log.e(tag, "event : up"); // if (mtouchstate == touch_state_scrolling) { final velocitytracker velocitytracker = mvelocitytracker; velocitytracker.computecurrentvelocity(1000); int velocityx = (int) velocitytracker.getxvelocity(); log.e(tag, "velocityx:" + velocityx); if (velocityx > snap_velocity && mcurscreen > 0) { // fling enough to move left log.e(tag, "snap left"); onscreenchangelistener.onscreenchange(mcurscreen - 1); system.out.println("mcurscreen=" + (mcurscreen - 1)); snaptoscreen(mcurscreen - 1); } else if (velocityx < -snap_velocity && mcurscreen < getchildcount() - 1) { // fling enough to move right log.e(tag, "snap right"); onscreenchangelistener.onscreenchange(mcurscreen + 1); //只往右移动才加载数据 onscreenchangelistenerdataload.onscreenchange(mcurscreen+1); snaptoscreen(mcurscreen + 1); } else { snaptodestination(); } if (mvelocitytracker != null) { mvelocitytracker.recycle(); mvelocitytracker = null; } // } mtouchstate = touch_state_rest; break; case motionevent.action_cancel: mtouchstate = touch_state_rest; break; } return true; } @override public boolean onintercepttouchevent(motionevent ev) { // todo auto-generated method stub log.e(tag, "onintercepttouchevent-slop:" + mtouchslop); final int action = ev.getaction(); if ((action == motionevent.action_move) && (mtouchstate != touch_state_rest)) { return true; } final float x = ev.getx(); final float y = ev.gety(); switch (action) { case motionevent.action_move: final int xdiff = (int) math.abs(mlastmotionx - x); if (xdiff > mtouchslop) { mtouchstate = touch_state_scrolling; } break; case motionevent.action_down: mlastmotionx = x; mlastmotiony = y; mtouchstate = mscroller.isfinished() ? touch_state_rest : touch_state_scrolling; break; case motionevent.action_cancel: case motionevent.action_up: mtouchstate = touch_state_rest; break; } return mtouchstate != touch_state_rest; } //分页监听 public interface onscreenchangelistener { void onscreenchange(int currentindex); } private onscreenchangelistener onscreenchangelistener; public void setonscreenchangelistener( onscreenchangelistener onscreenchangelistener) { this.onscreenchangelistener = onscreenchangelistener; } //动态数据监听 public interface onscreenchangelistenerdataload { void onscreenchange(int currentindex); } private onscreenchangelistenerdataload onscreenchangelistenerdataload; public void setonscreenchangelistenerdataload(onscreenchangelistenerdataload onscreenchangelistenerdataload) { this.onscreenchangelistenerdataload = onscreenchangelistenerdataload; } }
java代码
package cn.anycall.ju; import android.content.context; import android.graphics.canvas; import android.util.attributeset; import android.util.log; import android.view.motionevent; import android.view.velocitytracker; import android.view.view; import android.view.viewconfiguration; import android.view.viewgroup; import android.widget.scroller; /** * 仿launcher中的worksapce,可以左右滑动切换屏幕的类 * */ public class scrolllayout extends viewgroup { private static final string tag = "scrolllayout"; private scroller mscroller; private velocitytracker mvelocitytracker; private int mcurscreen; private int mdefaultscreen = 0; private static final int touch_state_rest = 0; private static final int touch_state_scrolling = 1; private static final int snap_velocity = 600; private int mtouchstate = touch_state_rest; private int mtouchslop; private float mlastmotionx; private float mlastmotiony; private int currentscreenindex = 0; public int getcurrentscreenindex() { return currentscreenindex; } public void setcurrentscreenindex(int currentscreenindex) { this.currentscreenindex = currentscreenindex; } public scrolllayout(context context, attributeset attrs) { this(context, attrs, 0); // todo auto-generated constructor stub } public scrolllayout(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); // todo auto-generated constructor stub mscroller = new scroller(context); mcurscreen = mdefaultscreen; mtouchslop = viewconfiguration.get(getcontext()).getscaledtouchslop(); } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { // todo auto-generated method stub int childleft = 0; final int childcount = getchildcount(); system.out.println("childcount=" + childcount); for (int i = 0; i < childcount; i++) { final view childview = getchildat(i); if (childview.getvisibility() != view.gone) { final int childwidth = childview.getmeasuredwidth(); childview.layout(childleft, 0, childleft + childwidth, childview.getmeasuredheight()); childleft += childwidth; } } } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { log.e(tag, "onmeasure"); super.onmeasure(widthmeasurespec, heightmeasurespec); final int width = measurespec.getsize(widthmeasurespec); final int widthmode = measurespec.getmode(widthmeasurespec); if (widthmode != measurespec.exactly) { throw new illegalstateexception( "scrolllayout only canmcurscreen run at exactly mode!"); } final int heightmode = measurespec.getmode(heightmeasurespec); if (heightmode != measurespec.exactly) { throw new illegalstateexception( "scrolllayout only can run at exactly mode!"); } // the children are given the same width and height as the scrolllayout final int count = getchildcount(); for (int i = 0; i < count; i++) { getchildat(i).measure(widthmeasurespec, heightmeasurespec); } system.out.println("moving to screen " + mcurscreen); scrollto(mcurscreen * width, 0); } /** * according to the position of current layout scroll to the destination * page. */ public void snaptodestination() { final int screenwidth = getwidth(); final int destscreen = (getscrollx() + screenwidth / 2) / screenwidth; snaptoscreen(destscreen); } public void snaptoscreen(int whichscreen) { // get the valid layout page whichscreen = math.max(0, math.min(whichscreen, getchildcount() - 1)); if (getscrollx() != (whichscreen * getwidth())) { final int delta = whichscreen * getwidth() - getscrollx(); mscroller.startscroll(getscrollx(), 0, delta, 0, math.abs(delta) * 2); mcurscreen = whichscreen; invalidate(); // redraw the layout } } public void settoscreen(int whichscreen) { whichscreen = math.max(0, math.min(whichscreen, getchildcount() - 1)); mcurscreen = whichscreen; scrollto(whichscreen * getwidth(), 0); } public int getcurscreen() { return mcurscreen; } @override public void computescroll() { // todo auto-generated method stub if (mscroller.computescrolloffset()) { scrollto(mscroller.getcurrx(), mscroller.getcurry()); postinvalidate(); } } @override public boolean ontouchevent(motionevent event) { // todo auto-generated method stub if (mvelocitytracker == null) { mvelocitytracker = velocitytracker.obtain(); } mvelocitytracker.addmovement(event); final int action = event.getaction(); final float x = event.getx(); final float y = event.gety(); switch (action) { case motionevent.action_down: log.e(tag, "event down!"); if (!mscroller.isfinished()) { mscroller.abortanimation(); } mlastmotionx = x; break; case motionevent.action_move: int deltax = (int) (mlastmotionx - x); mlastmotionx = x; scrollby(deltax, 0); break; case motionevent.action_up: log.e(tag, "event : up"); // if (mtouchstate == touch_state_scrolling) { final velocitytracker velocitytracker = mvelocitytracker; velocitytracker.computecurrentvelocity(1000); int velocityx = (int) velocitytracker.getxvelocity(); log.e(tag, "velocityx:" + velocityx); if (velocityx > snap_velocity && mcurscreen > 0) { // fling enough to move left log.e(tag, "snap left"); onscreenchangelistener.onscreenchange(mcurscreen - 1); system.out.println("mcurscreen=" + (mcurscreen - 1)); snaptoscreen(mcurscreen - 1); } else if (velocityx < -snap_velocity && mcurscreen < getchildcount() - 1) { // fling enough to move right log.e(tag, "snap right"); onscreenchangelistener.onscreenchange(mcurscreen + 1); //只往右移动才加载数据 onscreenchangelistenerdataload.onscreenchange(mcurscreen+1); snaptoscreen(mcurscreen + 1); } else { snaptodestination(); } if (mvelocitytracker != null) { mvelocitytracker.recycle(); mvelocitytracker = null; } // } mtouchstate = touch_state_rest; break; case motionevent.action_cancel: mtouchstate = touch_state_rest; break; } return true; } @override public boolean onintercepttouchevent(motionevent ev) { // todo auto-generated method stub log.e(tag, "onintercepttouchevent-slop:" + mtouchslop); final int action = ev.getaction(); if ((action == motionevent.action_move) && (mtouchstate != touch_state_rest)) { return true; } final float x = ev.getx(); final float y = ev.gety(); switch (action) { case motionevent.action_move: final int xdiff = (int) math.abs(mlastmotionx - x); if (xdiff > mtouchslop) { mtouchstate = touch_state_scrolling; } break; case motionevent.action_down: mlastmotionx = x; mlastmotiony = y; mtouchstate = mscroller.isfinished() ? touch_state_rest : touch_state_scrolling; break; case motionevent.action_cancel: case motionevent.action_up: mtouchstate = touch_state_rest; break; } return mtouchstate != touch_state_rest; } //分页监听 public interface onscreenchangelistener { void onscreenchange(int currentindex); } private onscreenchangelistener onscreenchangelistener; public void setonscreenchangelistener( onscreenchangelistener onscreenchangelistener) { this.onscreenchangelistener = onscreenchangelistener; } //动态数据监听 public interface onscreenchangelistenerdataload { void onscreenchange(int currentindex); } private onscreenchangelistenerdataload onscreenchangelistenerdataload; public void setonscreenchangelistenerdataload(onscreenchangelistenerdataload onscreenchangelistenerdataload) { this.onscreenchangelistenerdataload = onscreenchangelistenerdataload; } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="仿淘宝聚划算"/> <relativelayout android:id="@+id/myview" android:layout_width="fill_parent" android:layout_height="fill_parent" > <cn.anycall.ju.scrolllayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrolllayouttest" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#000000" > </cn.anycall.ju.scrolllayout> <cn.anycall.ju.pagecontrolview android:id="@+id/pagecontrol" android:layout_width="fill_parent" android:layout_height="40px" android:background="#8f00000f" android:layout_alignparentbottom="true" android:gravity="center"/> </relativelayout> </linearlayout>
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="仿淘宝聚划算"/> <relativelayout android:id="@+id/myview" android:layout_width="fill_parent" android:layout_height="fill_parent" > <cn.anycall.ju.scrolllayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrolllayouttest" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#000000" > </cn.anycall.ju.scrolllayout> <cn.anycall.ju.pagecontrolview android:id="@+id/pagecontrol" android:layout_width="fill_parent" android:layout_height="40px" android:background="#8f00000f" android:layout_alignparentbottom="true" android:gravity="center"/> </relativelayout> </linearlayout>
app_item.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <relativelayout android:id="@+id/alllayout" android:layout_width="wrap_content" android:layout_height="wrap_content"> <relativelayout android:id="@+id/imglayout" android:layout_width="160dp" android:layout_height="160dp" android:background="@drawable/mer_border"> <imageview android:id="@+id/imgdetail" android:layout_width="145dp" android:layout_height="145dp" android:layout_margin="8dp" /> <textview android:id="@+id/price" android:layout_width="180dp" android:layout_height="wrap_content" android:text="12345" android:layout_alignparentbottom="true" android:background="#c02000" android:textcolor="#ffffff"/> <textview android:id="@+id/look" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="去看看" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:background="#c02000" android:textcolor="#ffffff"/> </relativelayout> <textview android:id="@+id/tuaninfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="16dp" android:maxlines="2" android:layout_below="@id/imglayout" android:ellipsize="end" android:text="dddddddddd"/>" </relativelayout> </relativelayout>
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <relativelayout android:id="@+id/alllayout" android:layout_width="wrap_content" android:layout_height="wrap_content"> <relativelayout android:id="@+id/imglayout" android:layout_width="160dp" android:layout_height="160dp" android:background="@drawable/mer_border"> <imageview android:id="@+id/imgdetail" android:layout_width="145dp" android:layout_height="145dp" android:layout_margin="8dp" /> <textview android:id="@+id/price" android:layout_width="180dp" android:layout_height="wrap_content" android:text="12345" android:layout_alignparentbottom="true" android:background="#c02000" android:textcolor="#ffffff"/> <textview android:id="@+id/look" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="去看看" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:background="#c02000" android:textcolor="#ffffff"/> </relativelayout> <textview android:id="@+id/tuaninfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="16dp" android:maxlines="2" android:layout_below="@id/imglayout" android:ellipsize="end" android:text="dddddddddd"/>" </relativelayout> </relativelayout>
更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。