本文實例為大家分享了RecyclerView實現(xiàn)水波紋點擊效果的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),吉木乃企業(yè)網(wǎng)站建設(shè),吉木乃品牌網(wǎng)站建設(shè),網(wǎng)站定制,吉木乃網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,吉木乃網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
效果圖
item.xml
這里就是主要設(shè)置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水波紋效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。