本文实例为大家分享了RecyclerView实现水波纹点击效果的具体代码,供大家参考,具体内容如下
效果图
item.xml
这里就是主要设置background为我们后面写的一个selector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? 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版本以上)
1
2
3
4
5
|
<? 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版本以下的没有水波纹效果)
1
2
3
4
5
6
|
<? 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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水波纹效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/REIGE/article/details/68957994