Andriod学习教程之滑动布局(14)
程序员文章站
2024-03-01 20:59:46
本文实例为大家分享了andriod滑动布局的具体代码,供大家参考,具体内容如下
mainactivity.java代码:
package siso.swip...
本文实例为大家分享了andriod滑动布局的具体代码,供大家参考,具体内容如下
mainactivity.java代码:
package siso.swipelayoutdemo; import android.app.activity; import android.content.intent; import android.graphics.color; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.toast; import com.nineoldandroids.view.viewhelper; import siso.swipelib.swipelayout; public class mainactivity extends appcompatactivity { private swipelayout sample1, sample2, sample3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); sample1 = (swipelayout) findviewbyid(r.id.sample1); sample1.setshowmode(swipelayout.showmode.pullout); view starbottview = sample1.findviewbyid(r.id.starbott); sample1.adddrag(swipelayout.dragedge.left, sample1.findviewbyid(r.id.bottom_wrapper)); sample1.adddrag(swipelayout.dragedge.right, sample1.findviewbyid(r.id.bottom_wrapper_2)); sample1.adddrag(swipelayout.dragedge.top, starbottview); sample1.adddrag(swipelayout.dragedge.bottom, starbottview); sample1.addreveallistener(r.id.delete, new swipelayout.onreveallistener() { @override public void onreveal(view child, swipelayout.dragedge edge, float fraction, int distance) { } }); sample1.getsurfaceview().setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "click on surface", toast.length_short).show(); log.d(mainactivity.class.getname(), "click on surface"); } }); sample1.getsurfaceview().setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { toast.maketext(mainactivity.this, "longclick on surface", toast.length_short).show(); log.d(mainactivity.class.getname(), "longclick on surface"); return true; } }); sample1.findviewbyid(r.id.star2).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "star", toast.length_short).show(); } }); sample1.findviewbyid(r.id.trash2).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "trash bin", toast.length_short).show(); } }); sample1.findviewbyid(r.id.magnifier2).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "magnifier", toast.length_short).show(); } }); sample1.addreveallistener(r.id.starbott, new swipelayout.onreveallistener() { @override public void onreveal(view child, swipelayout.dragedge edge, float fraction, int distance) { view star = child.findviewbyid(r.id.star); float d = child.getheight() / 2 - star.getheight() / 2; viewhelper.settranslationy(star, d * fraction); viewhelper.setscalex(star, fraction + 0.6f); viewhelper.setscaley(star, fraction + 0.6f); } }); //sample2 sample2 = (swipelayout) findviewbyid(r.id.sample2); sample2.setshowmode(swipelayout.showmode.laydown); sample2.adddrag(swipelayout.dragedge.right, sample2.findviewwithtag("bottom2")); sample2.findviewbyid(r.id.star).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "star", toast.length_short).show(); } }); sample2.findviewbyid(r.id.trash).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "trash bin", toast.length_short).show(); } }); sample2.findviewbyid(r.id.magnifier).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "magnifier", toast.length_short).show(); } }); sample2.findviewbyid(r.id.click).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "yo", toast.length_short).show(); } }); sample2.getsurfaceview().setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "click on surface", toast.length_short).show(); } }); //sample3 sample3 = (swipelayout) findviewbyid(r.id.sample3); sample3.adddrag(swipelayout.dragedge.top, sample3.findviewwithtag("bottom3")); sample3.addreveallistener(r.id.bottom_wrapper_child1, new swipelayout.onreveallistener() { @override public void onreveal(view child, swipelayout.dragedge edge, float fraction, int distance) { view star = child.findviewbyid(r.id.star); float d = child.getheight() / 2 - star.getheight() / 2; viewhelper.settranslationy(star, d * fraction); viewhelper.setscalex(star, fraction + 0.6f); viewhelper.setscaley(star, fraction + 0.6f); int c = (integer) evaluate(fraction, color.parsecolor("#dddddd"), color.parsecolor("#4c535b")); child.setbackgroundcolor(c); } }); sample3.findviewbyid(r.id.bottom_wrapper_child1).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "yo!", toast.length_short).show(); } }); sample3.getsurfaceview().setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, "click on surface", toast.length_short).show(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.my, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_listview) { startactivity(new intent(this, listviewexample.class)); return true; } else if (id == r.id.action_gridview) { startactivity(new intent(this, gridviewexample.class)); return true; } else if (id == r.id.action_nested) { startactivity(new intent(this, nestedexample.class)); return true; } else if (id == r.id.action_recycler) { startactivity(new intent(this, recyclerviewexample.class)); } return super.onoptionsitemselected(item); } /* color transition method. */ public object evaluate(float fraction, object startvalue, object endvalue) { int startint = (integer) startvalue; int starta = (startint >> 24) & 0xff; int startr = (startint >> 16) & 0xff; int startg = (startint >> 8) & 0xff; int startb = startint & 0xff; int endint = (integer) endvalue; int enda = (endint >> 24) & 0xff; int endr = (endint >> 16) & 0xff; int endg = (endint >> 8) & 0xff; int endb = endint & 0xff; return (int) ((starta + (int) (fraction * (enda - starta))) << 24) | (int) ((startr + (int) (fraction * (endr - startr))) << 16) | (int) ((startg + (int) (fraction * (endg - startg))) << 8) | (int) ((startb + (int) (fraction * (endb - startb)))); } }
nestedexample.java代码:
package siso.swipelayoutdemo; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.toast; import siso.swipelib.swipelayout; public class nestedexample extends activity{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.complicate_layout); swipelayout swipelayout = (swipelayout)findviewbyid(r.id.test_swipe_swipe); swipelayout.setondoubleclicklistener(new swipelayout.doubleclicklistener() { @override public void ondoubleclick(swipelayout layout, boolean surface) { toast.maketext(getapplicationcontext(), "doubleclick", toast.length_short).show(); } }); swipelayout.findviewbyid(r.id.trash).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(getapplicationcontext(), "click", toast.length_short).show(); } }); } }
recyclerviewexample.java代码:
package siso.swipelayoutdemo; import android.app.actionbar; import android.app.activity; import android.content.intent; import android.os.build; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.util.log; import android.view.menu; import android.view.menuitem; import java.util.arraylist; import java.util.arrays; import jp.wasabeef.recyclerview.animators.fadeinleftanimator; import siso.swipelayoutdemo.adapter.recyclerviewadapter; import siso.swipelayoutdemo.adapter.util.divideritemdecoration; import siso.swipelib.util.attributes; public class recyclerviewexample extends activity { private recyclerview recyclerview; private recyclerview.adapter madapter; private arraylist<string> mdataset; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.recyclerview); recyclerview = (recyclerview) findviewbyid(r.id.recycler_view); if (build.version.sdk_int >= build.version_codes.honeycomb) { actionbar actionbar = getactionbar(); if (actionbar != null) { actionbar.settitle("recyclerview"); } } // layout managers: recyclerview.setlayoutmanager(new linearlayoutmanager(this)); // item decorator: recyclerview.additemdecoration(new divideritemdecoration(getresources().getdrawable(r.drawable.divider))); recyclerview.setitemanimator(new fadeinleftanimator()); // adapter: string[] adapterdata = new string[]{"alabama", "alaska", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "florida", "georgia", "hawaii", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota", "mississippi", "missouri", "montana", "nebraska", "nevada", "new hampshire", "new jersey", "new mexico", "new york", "north carolina", "north dakota", "ohio", "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina", "south dakota", "tennessee", "texas", "utah", "vermont", "virginia", "washington", "west virginia", "wisconsin", "wyoming"}; mdataset = new arraylist<string>(arrays.aslist(adapterdata)); madapter = new recyclerviewadapter(this, mdataset); ((recyclerviewadapter) madapter).setmode(attributes.mode.single); recyclerview.setadapter(madapter); /* listeners */ recyclerview.setonscrolllistener(onscrolllistener); } recyclerview.onscrolllistener onscrolllistener = new recyclerview.onscrolllistener() { @override public void onscrollstatechanged(recyclerview recyclerview, int newstate) { super.onscrollstatechanged(recyclerview, newstate); log.e("listview", "onscrollstatechanged"); } @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); } }; @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.my, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_listview) { startactivity(new intent(this, listviewexample.class)); finish(); return true; } else if (id == r.id.action_gridview) { startactivity(new intent(this, gridviewexample.class)); finish(); return true; } return super.onoptionsitemselected(item); } }
listviewexample.java代码:
package siso.swipelayoutdemo; import android.app.actionbar; import android.app.activity; import android.content.context; import android.content.intent; import android.os.build; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.motionevent; import android.view.view; import android.widget.abslistview; import android.widget.adapterview; import android.widget.listview; import android.widget.toast; import siso.swipelayoutdemo.adapter.listviewadapter; import siso.swipelib.swipelayout; import siso.swipelib.util.attributes; public class listviewexample extends activity { private listview mlistview; private listviewadapter madapter; private context mcontext = this; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.listview); mlistview = (listview) findviewbyid(r.id.listview); if (build.version.sdk_int >= build.version_codes.honeycomb) { actionbar actionbar = getactionbar(); if (actionbar != null) { actionbar.settitle("listview"); } } madapter = new listviewadapter(this); mlistview.setadapter(madapter); madapter.setmode(attributes.mode.single); mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { ((swipelayout)(mlistview.getchildat(position - mlistview.getfirstvisibleposition()))).open(true); } }); mlistview.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { log.e("listview", "ontouch"); return false; } }); mlistview.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> parent, view view, int position, long id) { toast.maketext(mcontext, "onitemlongclicklistener", toast.length_short).show(); return true; } }); mlistview.setonscrolllistener(new abslistview.onscrolllistener() { @override public void onscrollstatechanged(abslistview view, int scrollstate) { log.e("listview", "onscrollstatechanged"); } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { } }); mlistview.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { log.e("listview", "onitemselected:" + position); } @override public void onnothingselected(adapterview<?> parent) { log.e("listview", "onnothingselected:"); } }); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.my, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_listview) { startactivity(new intent(this, listviewexample.class)); finish(); return true; } else if (id == r.id.action_gridview) { startactivity(new intent(this, gridviewexample.class)); finish(); return true; } else if (id == r.id.action_recycler) { startactivity(new intent(this, recyclerviewexample.class)); finish(); return true; } return super.onoptionsitemselected(item); } }
gridviewexample.java代码:
package siso.swipelayoutdemo; import android.app.activity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.adapterview; import android.widget.gridview; import siso.swipelayoutdemo.adapter.gridviewadapter; import siso.swipelib.util.attributes; public class gridviewexample extends activity{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.gridview); final gridview gridview = (gridview)findviewbyid(r.id.gridview); final gridviewadapter adapter = new gridviewadapter(this); adapter.setmode(attributes.mode.multiple); gridview.setadapter(adapter); gridview.setselected(false); gridview.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> parent, view view, int position, long id) { log.e("onitemlongclick","onitemlongclick:" + position); return false; } }); gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { log.e("onitemclick","onitemclick:" + position); } }); gridview.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { log.e("onitemselected","onitemselected:" + position); } @override public void onnothingselected(adapterview<?> parent) { } }); } }
app项目结构:
andriod类库项目结构:
build.gradle
apply plugin: 'com.android.application' android { compilesdkversion 23 buildtoolsversion "23.0.1" defaultconfig { applicationid "siso.swipelayoutdemo" minsdkversion 22 targetsdkversion 22 versioncode 1 versionname "1.0" } buildtypes { release { minifyenabled false proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile filetree(include: ['*.jar'], dir: 'libs') testcompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.0.1' compile project(path: ':swipelib') compile 'com.android.support:recyclerview-v7:23.3.0' compile 'com.daimajia.easing:library:1.0.0@aar' compile 'com.daimajia.androidanimations:library:1.1.2@aar' compile 'com.nineoldandroids:library:2.4.0' compile 'jp.wasabeef:recyclerview-animators:1.0.3@aar' }
运行结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java开发学习 Java数组操作工具
下一篇: Java IO中字节流复制图片实现代码