RecyclerView实现水波纹点击效果
程序员文章站
2022-03-23 13:36:57
本文实例为大家分享了recyclerview实现水波纹点击效果的具体代码,供大家参考,具体内容如下
效果图
item.xml
这里就是主要设置backgroun...
本文实例为大家分享了recyclerview实现水波纹点击效果的具体代码,供大家参考,具体内容如下
效果图
item.xml
这里就是主要设置background为我们后面写的一个selector
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/recycler_item_selector" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textsize="40sp" android:text="aaaa"/> </linearlayout>
drawable-v21/recycler_item_selector.xml (v21版本以上)
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorprimary"> <item android:drawable="@color/write"/> </ripple>
drawable/recycler_item_selector.xml (v21版本以下的没有水波纹效果)
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/colorprimary" android:state_pressed="true"/> <item android:drawable="@color/colorprimary" android:state_focused="true"/> <item android:drawable="@color/write"/> </selector>
接下来是myadapter
public class myadapter extends recyclerview.adapter<myadapter.viewholder> { private onrvitemclick monrvitemclick; private context mcontext; string[] strings; public myadapter(context ctx, string[] strings, onrvitemclick onrvitemclick) { mcontext = ctx; this.monrvitemclick = onrvitemclick; this.strings = strings; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(mcontext).inflate(r.layout.item, parent, false); return new viewholder(view); } @override public void onbindviewholder(viewholder holder, int position) { holder.setdata(position); } @override public int getitemcount() { return 20; } public class viewholder extends recyclerview.viewholder implements view.onclicklistener { @bindview(r.id.textview) textview textview; public viewholder(view itemview) { super(itemview); butterknife.bind(this,itemview); itemview.setonclicklistener(this); } public void setdata(int position) { textview.settext("position" + position); } @override public void onclick(view view) { if (monrvitemclick != null) monrvitemclick.onitemclick(view, getadapterposition()); } } /** * item点击接口 */ public interface onrvitemclick { void onitemclick(view v, int position); } }
mainactivity
public class mainactivity extends appcompatactivity { @bindview(r.id.recyclerview) recyclerview mrecyclerview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); butterknife.bind(this); myadapter myadapter = new myadapter(this, null, new myadapter.onrvitemclick() { @override public void onitemclick(view v, int position) { toast.maketext(getapplicationcontext(), "click" + position, toast.length_short) .show(); } }); mrecyclerview.setlayoutmanager(new linearlayoutmanager(this,linearlayoutmanager.vertical,false)); mrecyclerview.setadapter(myadapter); } }
github :recyclerview水波纹效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: MySQL不停地自动重启怎么办
推荐阅读
-
JS实现的点击按钮图片上下滚动效果示例
-
Android Recyclerview实现水平分页GridView效果示例
-
Android自定义View实现水波纹效果
-
Android中RecyclerView实现多级折叠列表效果(二)
-
Android中RecyclerView实现多级折叠列表效果(TreeRecyclerView)
-
Android 使用自定义RecyclerView控件实现Gallery效果
-
recyclerview item点击事件无效(recyclerview实现双击)
-
详解Android实现购物车页面及购物车效果(点击动画)
-
Android RecyclerView打造悬浮效果的实现代码
-
Android 点击ImageButton时有“按下”的效果的实现