问题
Basically what the title says,
My application consists primarily of a ViewPager which uses a FragmentStatePagerAdapter
The problem occurs when I add a series of SurfaceViews to the FragmentStatePagerAdapter. Just for testing purposes I did not subclass the SurfaceViews in any way. When I navigate a few pages in, hit "home," and then return to the activity, the entire device freezes for a second or two before rendering anything. I can launch and finish() the app several times in the time it takes for it to resume after being pulled off of the backstack. I know that the application is running because several AsyncTasks have nearly finished by the time the first screendraw finally occurs. I know that these AsyncTasks do not cause the problem because I have removed them to no effect.
When I change the SurfaceViews to Views the problem vanishes entirely.
This problem occurs when I use my actual SurfaceViews (which are non-negotiable for my project) as well. This problem also occurs on multiple devices.
At this point I think the fault lies squarely on the ViewPager, but I don't know what to do about it because I need that interface component.
Any help would be greatly appreciated!
EDIT:
To clarify, the SurfaceViews/Views represent the View of the Fragment which was added to the FragmentStatePagerAdapter.
回答1:
Found the solution for any future googlers!
Heres how I did it:
In my fragment I instantiated mySurfaceView in onCreate()
Then I attached a dummy layout (LinearLayout) to the actual view during onCreateView()
Then in onResume() I cleared the dummy layout with removeAllViews() and then called addView(mySurfaceView)
This seems to allow the ViewPager to build its layout quickly and then the SurfaceView is just popped in before the user actually needs to see anything!
回答2:
The better solution I found is to
- Override
getItem()
inFragmentPageAdapter
- When returning a
Fragment
create a 2 sec time delay usingHandler
That's it now everytime your Fragment page adapter tries to return SurfaceView
it will wait for 2 sec or whatever. And Surface View will be initaited in that time. below it is shown
fragmentAdapter extends FragmentPageAdapter
...
@Override
getItem(int position)
...
if (position == 0)
> final Handler handler = new Handler();
> handler.postDelayed(new Runnable() {
> @Override
> public void run() {
> // initiate SurfaceView
> // return fragment after 5s
> }
> }, 5000);
来源:https://stackoverflow.com/questions/11356085/viewpager-surfaceview-long-delay-when-navigating-back-to-activity