RecyclerView实现水波纹点击效果
本文实例为大家分享了RecyclerView实现水波纹点击效果的具体代码,供大家参考,具体内容如下
创新互联是一家集网站建设,吉木乃企业网站建设,吉木乃品牌网站建设,网站定制,吉木乃网站建设报价,网络营销,网络优化,吉木乃网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
效果图
item.xml
这里就是主要设置background为我们后面写的一个selector
<?xml version="1.0" encoding="utf-8"?>
drawable-v21/recycler_item_selector.xml (v21版本以上)
<?xml version="1.0" encoding="utf-8"?>
drawable/recycler_item_selector.xml (v21版本以下的没有水波纹效果)
<?xml version="1.0" encoding="utf-8"?>
接下来是MyAdapter
public class MyAdapter extends RecyclerView.Adapter{ 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水波纹效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
文章名称:RecyclerView实现水波纹点击效果
当前URL:http://scjbc.cn/article/giisdp.html