Codey
Codey

Reputation: 460

Add animation to swipe and stay in the same activity

I have a fragment that show data according to date the user choose, and the user can move to the next day or the previous one. how can I add an animation when the user change the day like he move to another page? I mean, stay in the same fragment but with different data after the animation and.

This is my method to change date

 mIvNext.setOnClickListener(view -> {
        calendar.add(Calendar.DATE,+1);
        scrollToDays(calendar.getTime());
    });
    mIvPrev.setOnClickListener(view -> {
        calendar.add(Calendar.DATE,-1);
        scrollToDays(calendar.getTime());

    });

Thanks

Upvotes: 1

Views: 847

Answers (1)

RaghavPai
RaghavPai

Reputation: 672

Hi Please check the below example let me know if any issues.

package example.fragmentanim;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

/**
 * Created by Raghav Pai on 12-03-2017.
 */

public class MainActivity extends AppCompatActivity {

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

The Xml for the activity activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="hubble.fragmentanim.MainActivity">

    <fragment
        android:id="@+id/headlines_fragment"
        android:name="example.fragmentanim.HeadlinesFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

The single fragment loaded for activity HeadlinesFragment.java

package example.fragmentanim;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
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.LinearLayout;
import android.widget.TextView;

import java.util.Calendar;

/**
 * Created by Raghav Pai on 12-03-2017.
 */
public class HeadlinesFragment extends Fragment {
    private LinearLayout mAnimator;
    private Button mLeft;
    private Button mRight;
    private Animation mLeftAnim;
    private Animation mRightAnim;
    private TextView mDateText;
    private Calendar calendar = Calendar.getInstance();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.article_view, container, false);

        mAnimator = (LinearLayout) view.findViewById(R.id.animator);
        mLeft = (Button) view.findViewById(R.id.left);
        mRight = (Button) view.findViewById(R.id.right);
        mDateText = (TextView) view.findViewById(R.id.date);

        mLeftAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in_left);
        mRightAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in_right);
        mDateText.setText(calendar.getTime().toString());

        mLeftAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                calendar.add(Calendar.DATE,-1);
                mDateText.setText(calendar.getTime().toString());
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        mRightAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                calendar.add(Calendar.DATE,+1);
                mDateText.setText(calendar.getTime().toString());
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        mLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAnimator.startAnimation(mLeftAnim);
            }
        });

        mRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAnimator.startAnimation(mRightAnim);
            }
        });
        return view;
    }

}

The xml for the fragment article_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/animator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="40dp"
            android:gravity="center"
            android:text="Date"
            android:textSize="20dp" />

        <Button
            android:id="@+id/left"
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Left" />

        <Button
            android:id="@+id/right"
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Right" />
    </LinearLayout>

</LinearLayout>

The anim files slide_in_left.xml & slide_in_right.xml in res/anim folder

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:toXDelta="-100%" />
    />
</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:toXDelta="100%" />
    />
</set>

manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.fragmentanim">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Best Regds

Upvotes: 4

Related Questions