Android编程实现ListView头部ViewPager广告轮询图效果
程序员文章站
2023-11-23 21:17:22
本文实例讲述了android编程实现listview头部viewpager广告轮询图效果。分享给大家供大家参考,具体如下:
之前看了别人的一些软件,发现其广告图轮询的时候...
本文实例讲述了android编程实现listview头部viewpager广告轮询图效果。分享给大家供大家参考,具体如下:
之前看了别人的一些软件,发现其广告图轮询的时候,那个广告感觉和viewpager的效果不太一样,后来也查了一下,是因为时间问题,找了一些资料,自己也实践一下。
1、为了解决listview头部加viewpager滑动冲突问题,必须自定义listview,重写里面的onintercepttouchevent方法,listview代码如下:
package com.example.testa; import android.content.context; import android.util.attributeset; import android.view.motionevent; import android.widget.listview; public class definedlistview extends listview { public definedlistview(context context) { super(context); } public definedlistview(context context, attributeset attrs) { super(context, attrs); } public definedlistview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } private float xdistance, ydistance, xlast, ylast; @override public boolean onintercepttouchevent(motionevent ev) { switch (ev.getaction()) { case motionevent.action_down: xdistance = ydistance = 0f; xlast = ev.getx(); ylast = ev.gety(); break; case motionevent.action_move: final float curx = ev.getx(); final float cury = ev.gety(); xdistance += math.abs(curx - xlast); ydistance += math.abs(cury - ylast); xlast = curx; ylast = cury; if (xdistance > ydistance) { return false; } } return super.onintercepttouchevent(ev); } }
2、为了让广告轮询的速度减慢,必须重新设置viewpager的scroller,自定义的scroller如下:
package com.example.testa; import java.lang.reflect.field; import android.annotation.suppresslint; import android.content.context; import android.os.handler; import android.support.v4.app.fragmentactivity; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.view.animation.interpolator; import android.widget.scroller; public class definedscroller extends scroller { private int duration_time = 1000; private boolean shouldautoscroll = true; private viewpager viewpager = null; @suppresslint("handlerleak") private final handler handler = new handler() { public void handlemessage(android.os.message msg) { if (shouldautoscroll) { viewpager.setcurrentitem(viewpager.getcurrentitem() + 1); handler.sendemptymessagedelayed(0, 2000); } }; }; public definedscroller(context context) { super(context); } public definedscroller(context context, interpolator interpolator) { super(context, interpolator); } private view initviewpager(context context) { view view = layoutinflater.from(context).inflate(r.layout.view_pager_layout, null); this.viewpager = (viewpager) view.findviewbyid(r.id.viewpager); this.viewpager.setoffscreenpagelimit(2); this.viewpager.setadapter(new myfragmentpageradapter(((fragmentactivity) context).getsupportfragmentmanager())); return view; } public viewpager getviewpager() { return viewpager; } public view initview(context context) { view view = initviewpager(context); this.viewpager.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (event.getaction() == motionevent.action_up) { shouldautoscroll = true; handler.sendemptymessagedelayed(0, 2000); } else { handler.removemessages(0); shouldautoscroll = false; } return false; } }); try { field mscroller = viewpager.class.getdeclaredfield("mscroller"); mscroller.setaccessible(true); mscroller.set(viewpager, this); } catch (nosuchfieldexception e) { } catch (illegalargumentexception e) { } catch (illegalaccessexception e) { } handler.sendemptymessagedelayed(0, 2000); return view; } public void setshouldautoscroll() { handler.removemessages(0); if (viewpager.isshown()) { shouldautoscroll = true; handler.sendemptymessagedelayed(0, 2000); } else { shouldautoscroll = false; } } @override public void startscroll(int startx, int starty, int dx, int dy, int duration) { super.startscroll(startx, starty, dx, dy, shouldautoscroll ? duration_time : duration); } }
关键是startscroll方法啦
3、其他测试类,
(1)testfragment:
package com.example.testa; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; public class testfragment extends fragment { public int imageresource = 0; public static testfragment newinstance(int imageresource) { testfragment newfragment = new testfragment(); newfragment.imageresource = imageresource; return newfragment; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.pager_layout, null); imageview textview = (imageview) view.findviewbyid(r.id.image); textview.setimageresource(imageresource); return view; } }
(2)viewpager的adapter类:
package com.example.testa; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentstatepageradapter; public class myfragmentpageradapter extends fragmentstatepageradapter { public myfragmentpageradapter(fragmentmanager fm) { super(fm); } @override public int getcount() { return integer.max_value; } @override public fragment getitem(int arg0) { switch (arg0 % 4) { case 0: return testfragment.newinstance(r.drawable.a1); case 1: return testfragment.newinstance(r.drawable.default_wh); case 2: return testfragment.newinstance(r.drawable.default_ww); case 3: return testfragment.newinstance(r.drawable.test); default: return testfragment.newinstance(r.drawable.a1); } } }
(3)测试的activity:
package com.example.testa; import java.util.arraylist; import java.util.list; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.abslistview; import android.widget.abslistview.onscrolllistener; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.baseadapter; import android.widget.textview; public class mainactivity extends fragmentactivity { private definedlistview listview = null; private list<string> list = null; private definedscroller definedscroller = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (definedlistview) findviewbyid(r.id.listview); initlistdata(); initviewpager(); listview.setadapter(new listadapter()); listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { definedscroller.getviewpager().setcurrentitem(definedscroller.getviewpager().getcurrentitem() + 1); } }); listview.setonscrolllistener(new onscrolllistener() { @override public void onscrollstatechanged(abslistview view, int scrollstate) { if (scrollstate == scroll_state_idle) { definedscroller.setshouldautoscroll(); } } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { } }); } private void initlistdata() { list = new arraylist<string>(); for (int i = 1; i <= 20; ++i) { list.add("test" + i); } } private void initviewpager() { definedscroller = new definedscroller(this); listview.addheaderview(definedscroller.initview(this)); } private class listadapter extends baseadapter { @override public int getcount() { return list.size(); } @override public object getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { textview textview = (textview) layoutinflater.from(mainactivity.this).inflate(r.layout.text_view_layout, null); textview.setpadding(20, 20, 20, 20); textview.settext(list.get(position)); return textview; } } }
4、布局文件就算了吧
上面有些是在别的地方看到的,很久了,忘记在哪里看到的了,就写不上源地址了,敬请见谅。
希望本文所述对大家android程序设计有所帮助。