Gab*_*ito 9 android ripple material-design android-cardview android-recyclerview
我想添加一些点击动画如下所示我回收的看法,但没有成功.这是我的一些代码.
在我的活动中
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(this, R.dimen.card_item_offset);
mRecyclerView.addItemDecoration(itemDecoration);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new CustomAdapter(this);
mRecyclerView.setAdapter(mAdapter);
}
Run Code Online (Sandbox Code Playgroud)
在我的ViewHolder中
public CustomViewHolder(View itemView) {
super(itemView);
this.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* on click stuff here */
}
});
}
Run Code Online (Sandbox Code Playgroud)
在我的CardView.xml里面
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<TextView
android:id="@+id/card_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
不确定问题出在哪里,但提前感谢您帮助=)
Adi*_*han 30
这被称为涟漪效应,你正在尝试用背景代替前景
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
你也可以自定义
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorcode">
<item
android:id="@android:id/mask"
android:drawable="@color/colorcode" />
</ripple>
Run Code Online (Sandbox Code Playgroud)
为了支持旧版本,你可以这样做
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@color/colorcode" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/colorcode" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13392 次 |
| 最近记录: |