我有一个片段,根据用户选择的日期显示数据,用户可以移动到第二天或前一天。 如何在用户更改日期时添加动画,就像他移动到另一个页面一样?我的意思是,在动画之后保持相同的片段但是具有不同的数据。
这是我改变日期的方法
mIvNext.setOnClickListener(view -> {
calendar.add(Calendar.DATE,+1);
scrollToDays(calendar.getTime());
});
mIvPrev.setOnClickListener(view -> {
calendar.add(Calendar.DATE,-1);
scrollToDays(calendar.getTime());
});
由于
答案 0 :(得分:3)
嗨请检查以下示例,如果有任何问题,请告诉我。
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);
}
}
活动activity_main.xml的 为活动HeadlinesFragment.java 片段article_view.xml的 动画文件slide_in_left.xml&amp; res / anim文件夹中的slide_in_right.xml 清单文件 Best Regds <?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>
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;
}
}
<?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>
<?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>
<?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>