slide views which are within view animator from right to left for previous views

595 Views Asked by At

I want to slide views which are within ViewAnimator from right to left for previous views.

This is my 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="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/btn_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PREVIOUS" />

        <Button
            android:id="@+id/btn_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="NEXT" />
    </LinearLayout>

    <ViewAnimator
        android:id="@+id/view_animator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/btn_layout">

    </ViewAnimator>


</RelativeLayout>

This is my Activity

package com.example.ronem.testing;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewAnimator;

/**
 * Created by ronem on 3/17/16.
 */
public class ViewAnimatorActivity extends AppCompatActivity {
    Button btnPrev, btnNext;
    ViewAnimator viewAnimator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_animator_layout);

        btnPrev = (Button) findViewById(R.id.btn_previous);
        btnNext = (Button) findViewById(R.id.btn_next);
        viewAnimator = (ViewAnimator) findViewById(R.id.view_animator);

        addImageView();

        Animation inAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
        Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);

        viewAnimator.setInAnimation(inAnimation);
        viewAnimator.setOutAnimation(outAnimation);


        btnPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewAnimator.showPrevious();
            }
        });

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewAnimator.showNext();
            }
        });
    }

    private void addImageView() {
        int[] drawable = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.images};

        for (int i = 0; i < drawable.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(drawable[i]);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
            viewAnimator.addView(imageView);
        }
    }
}

Whenever I click on Next button imageView slides from left to right that works fine, But when I click on previous button it shows the previous imageView which seems fine but only problem is that the image view is sliding from left to right again but I want to slide image from right to left when I click on previous button. What attribute should I add to ViewAnimator ? any suggestions will be helpful.

1

There are 1 best solutions below

0
Angienator On

In the next button click listener, set the in/out animations the way you did originally. In the previous button click listener, set the in/out animations the opposite way. That way they will go the desired direction based on which button you click.