单击时提升 LinearLayout

Lift LinearLayout on click

res/animator 目录中我有 lift_on_touch.xml 文件,它应该在点击时提升线性布局(不是 按下或按住但只是短 单击 )。文件本身

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <objectAnimator android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="elevation"
            android:valueTo="6dp"
            android:valueType="floatType" />
    </item>
    <item>
        <objectAnimator android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="elevation"
            android:valueTo="2dp"
            android:valueType="floatType" />
    </item>
</selector>

但是当我将 stateListAnimator 应用到布局 xml 中的线性布局时,像这样 android:stateListAnimator="@animator/lift_on_touch" 它只在我按住我的线性布局时才起作用(即提升) 但不是 当我点击它的时候。我的线性布局中也有 android:clickable="true"

问题:如何在点击时提升我的线性布局

i think you should use onTouchListner() instead of onClickListner bcs here you can get the event of Hold and unHold of View by using below code

linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = MotionEventCompat.getActionMasked(event);

                switch(action) {
                    case (MotionEvent.ACTION_DOWN) :
                        Log.d(DEBUG_TAG, "Action was DOWN");
                        // Hold Event will invoke continues 
                        return true;
                    case (MotionEvent.ACTION_MOVE) :
                        Log.d(DEBUG_TAG,"Action was MOVE");
                        return true;
                    case (MotionEvent.ACTION_UP) :
                        Log.d(DEBUG_TAG,"Action was UP");
                        // here unhold event will invoke 
                        return true;
                    case (MotionEvent.ACTION_CANCEL) :
                        Log.d(DEBUG_TAG,"Action was CANCEL");
                        return true;
                    case (MotionEvent.ACTION_OUTSIDE) :
                        Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                                "of current screen element");
                        return true;

                }
                return false;
            }
        });