欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

RecyclerView实现水波纹点击效果

程序员文章站 2022-03-23 13:36:57
本文实例为大家分享了recyclerview实现水波纹点击效果的具体代码,供大家参考,具体内容如下 效果图 item.xml 这里就是主要设置backgroun...

本文实例为大家分享了recyclerview实现水波纹点击效果的具体代码,供大家参考,具体内容如下

效果图

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水波纹效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。