Android CustomAdapter使动画运行不平稳
我已经有这个问题很长一段时间了,经过一个又一个小时的努力寻找解决方案,我决定尝试在这里征求一些建议 我有一个片段,基本上是一个日历。它由适配器填充的GridView组成(它还突出显示SQLite数据库中有数据的日期)。主要问题是,当设置此适配器时,它会将屏幕“冻结”几秒钟,并使动画不平滑 动画运行不平稳的一个例子是当我单击滑动菜单中的一个按钮时(jfeinstein10)。当我单击用日历片段替换当前片段的按钮时,滑动菜单中的滑出动画不会平稳运行。经过长时间的寻找,我发现是在我将适配器设置为GridView(Android CustomAdapter使动画运行不平稳,android,animation,android-fragments,smooth,Android,Animation,Android Fragments,Smooth,我已经有这个问题很长一段时间了,经过一个又一个小时的努力寻找解决方案,我决定尝试在这里征求一些建议 我有一个片段,基本上是一个日历。它由适配器填充的GridView组成(它还突出显示SQLite数据库中有数据的日期)。主要问题是,当设置此适配器时,它会将屏幕“冻结”几秒钟,并使动画不平滑 动画运行不平稳的一个例子是当我单击滑动菜单中的一个按钮时(jfeinstein10)。当我单击用日历片段替换当前片段的按钮时,滑动菜单中的滑出动画不会平稳运行。经过长时间的寻找,我发现是在我将适配器设置为Gri
GridView.setAdapter(adapter);
)时发现的
我已经尝试了一个异步任务,但没有结果
所以我的问题是如何设置适配器,使其不影响设置时发生的滑入动画
日历片段的一些代码供参考:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
context = getActivity();
month = (GregorianCalendar) GregorianCalendar.getInstance();
month.setFirstDayOfWeek(GregorianCalendar.MONDAY);
itemmonth = (GregorianCalendar) month.clone();
items = new ArrayList<String>();
adapter = new CalendarAdapter(context, month);
gridview = (GridView) view.findViewById(R.id.calendarGridView);
gridview.setAdapter(adapter);
handler = new Handler();
handler.post(calendarUpdater);
title = (TextView) view.findViewById(R.id.currentMonthTextView);
title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
previous = (Button) view.findViewById(R.id.prevMonthButton);
previous.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setPreviousMonth();
refreshCalendar();
}
});
[the code continues but I don't think it's relevant]
}
在主要活动中:
public void switchContent(Fragment fragment) {
mContent = fragment;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
getSlidingMenu().showContent();
}
这个问题使得用户过期非常糟糕,因此如果您有任何想法,请告诉我。我已经找到了解决方案。我已经覆盖了片段的
onCreateAnimation
方法,并在动画的onAnimationEnd
方法中设置了适配器。像这样:
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
Animation anim;
if (enter) {
anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
} else {
anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out);
}
anim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
gridview.setAdapter(adapter);
}
public void onAnimationRepeat(Animation animation) { }
public void onAnimationStart(Animation animation) { }
});
return anim;
}
这样,片段有一个淡入淡出动画,其持续时间与菜单中的滑出时间相同或更多,因此设置适配器后,动画不会运行,因此,动画运行平稳。是否使用了traceview?具体在哪里?无论如何,我找到了一个解决方案,在片段中重写
onCreateAnimation
方法,并在onAnimationEnd
方法中设置适配器
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
Animation anim;
if (enter) {
anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
} else {
anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out);
}
anim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
gridview.setAdapter(adapter);
}
public void onAnimationRepeat(Animation animation) { }
public void onAnimationStart(Animation animation) { }
});
return anim;
}