问题描述
我见过这里一些类似的职位,但没有合适的答案。
I've seen some similar posts here but no appropriate answer.
我使用事件在viewpager ,以执行一个功能使用onPageSelected。此功能是依赖于网络,因此可能需要一些时间。但随后的幻灯片动画看起来laggy。
I use the event onpageselected in a viewpager to execute a function. This function is network dependent so it can take some time. But then the slide animation looks laggy.
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
makeSomeHeavyTask();
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
繁重的任务:
public void makeSomeHeavyTask() {
handler.post(new QueryRunnable());
}
有没有办法解决这个问题的方法吗?谢谢
Is there a way to solve this? Thanks
推荐答案
执行任务繁重与的AsyncTask
。这里的AsyncTask:其中,doInBackground()的返回值去?
Do the heavy task with an AsyncTask
. Check out the accepted answer here AsyncTask: where return value of doInBackground() goes?
编辑:您需要在 getView
先返回的占位符。如果 getView
方法等待线程完成,那么动画将滞后是肯定的。返回的占位符以后可以用来做张贴线程完成之后。
You need to return a placeholder in the getView
first. If the getView
method has to wait for the thread to finish, then the animation will lag for sure. The returned placeholder can later be used to do posting after thread has finished.
这篇关于安卓:Viewpager OnPageChangeListener使用onPageSelected扰乱幻灯片动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!