1

I have a RecyclerView, that has a RippleEffect as well as a StateListAnimator (which is shown below):

anim_lift.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:state_pressed="true">
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="8dp"
                android:valueType="floatType"/>
        </set>
    </item>
    <item>
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="0"
                android:valueType="floatType"/>
        </set>
    </item>
</selector>

The question is, how would one elevate the view that is using this xml when it is clicked, and then de-elevate it when the view is clicked again.

TejjD
  • 2,571
  • 1
  • 17
  • 37
  • http://stackoverflow.com/questions/24458615/how-to-use-statelistanimator – Raghunandan Jan 12 '16 at 08:49
  • That is a StateListAnimator. i.e, it will animate an elevation then animate it off. I would like for the elevation to be permanent after the initial animation – TejjD Jan 12 '16 at 08:51
  • You can also used different color when pressing set one color and again press on that color will be change that type of logic also applied... – Ricky Patel Jan 12 '16 at 10:40
  • That isn't the desired outcome. The result needs to be for the view to be elevated once selected. – TejjD Jan 12 '16 at 13:21

1 Answers1

2

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(new View.OnClickListener() {
    public void onClick(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":

<item android:state_selected="true">
    <set>
        <objectAnimator
            android: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.

Floern
  • 33,559
  • 24
  • 104
  • 119
  • Ah it works, but not where I need to use it. If you refer here http://stackoverflow.com/questions/34673988/android-include-with-rippleeffect-statelistanimator, I need to elevate each item that is selected. I will mark correct as you've answered my question. could you assist me further? Regarding the elevation of the items in that question. Thanks – TejjD Jan 15 '16 at 07:01