1

I am trying to have a linear layout view slide in from the right and stop at the halfway point of the list view, however with my current animation code it goes further out than the width expected. Also, when the collapse takes place, it kind of works but re-shows the static view then just disappears...

This is the animation class :

public class MyCustomAnimation extends Animation {

    public final static int COLLAPSE = 1;
    public final static int EXPAND = 0;

    private View mView;
    private int mEndWidth;
    private int mType;
    private LinearLayout.LayoutParams mLayoutParams;

    public MyCustomAnimation(View view, int duration, int type) {

        setDuration(duration);
        mView = view;
        mEndWidth = mView.getWidth();
        mLayoutParams = ((LinearLayout.LayoutParams) view.getLayoutParams());
        mType = type;
        if(mType == EXPAND) {
            mLayoutParams.width = 0;
        } else {
            mLayoutParams.width = LayoutParams.WRAP_CONTENT;
        }
        view.setVisibility(View.VISIBLE);
    }

    public int getWidth(){
        return mView.getWidth();
    }

    public void setWidth(int width){
        mEndWidth = width;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1.0f) {
            if(mType == EXPAND) {
                mLayoutParams.width =  (int)(mEndWidth * interpolatedTime);
            } else {
                mLayoutParams.width = (int) (mEndWidth * (1 - interpolatedTime));
            }
            mView.requestLayout();
        } else {
            if(mType == EXPAND) {
                mLayoutParams.width = LayoutParams.WRAP_CONTENT;
                mView.requestLayout();
            }else{
                mView.setVisibility(View.GONE);
            }
        }
    }
}

taken from answers found on stackoverflow: Animate view sliding out of another view, pushing views below out of the way

this is how I try to implement it in the list view:

holder.homeInfoBtn.setOnClickListener(new View.OnClickListener() {
            int width;
            @Override
            public void onClick(View v) {
               // holder.homeProductInfo.setVisibility(View.VISIBLE);
                //holder.homeInfoBtn.setVisibility(View.GONE);
                MyCustomAnimation a = new MyCustomAnimation(holder.homeProductInfo, 1000, MyCustomAnimation.EXPAND);
                width = a.getWidth();
                holder.homeProductInfo.startAnimation(a);
            }
        });

This is the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/home_list_image">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="@dimen/home_list_image"
        android:id="@+id/homeProductPic"
        android:src="@drawable/ic_person_black_18dp"/>


    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="10dp"
        android:id="@+id/homeInfoBtn"
        android:src="@drawable/ic_info_black_18dp"
        android:layout_alignParentRight="true"
        android:focusable="false"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/home_list_image"
        android:orientation="horizontal">

        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1" />

        <!-- Right side spacer -->
        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:background="@color/background_floating_material_dark"
            android:gravity="center_vertical"
            android:padding="5dp"
            android:id="@+id/homeProductInfo"
            android:animateLayoutChanges="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/homeName"
                android:text="Name/>

        </LinearLayout>

    </LinearLayout>



</RelativeLayout>
Community
  • 1
  • 1
Lion789
  • 4,402
  • 12
  • 58
  • 96

0 Answers0