Android: Permanently Elevate View
I have a RecyclerView, that has a RippleEffect as well as a StateListAnimator (which is shown below): anim_lift.xml
Solution 1:
This can be achieved by using the selected state of the View. If you click the View you setSelected(true)
and when you click it again setSelected(false)
.
yourView.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View v) {
v.setSelected(!v.isSelected()); // toggle selected state
}
});
The selected state then can be handled by the StateListAnimator. You may add a new item to catch state_selected="true"
:
<itemandroid:state_selected="true"><set><objectAnimatorandroid:duration="@android:integer/config_shortAnimTime"android:propertyName="translationZ"android:valueTo="8dp"android:valueType="floatType"/></set></item>
Now if the View is selected it will have an elevation of 8dp
. If it's not selected the StateListAnimator will fall through to the default elevation of 0dp
.
Post a Comment for "Android: Permanently Elevate View"