欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

RecyclerView实现水波纹点击效果

时间:2020-12-09 21:38:54|栏目:Android代码|点击:

本文实例为大家分享了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水波纹效果

上一篇:Android程序开发ListView+Json+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)

栏    目:Android代码

下一篇:浅析安卓(Android)的性能优化

本文标题:RecyclerView实现水波纹点击效果

本文地址:http://www.codeinn.net/misctech/31161.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有