I've already read several threads about this problem but I got another strange misbehavior:
I've created a ViewPager that has an initial Page count of 5. If the page count-2 is reached,
pagerAdapter.notifyDataSetChanged();
gets called. The fragments all get their own id, counting up from 1. So the first fragment got the number one, second one the number two and so on. Each fragment loads an image out of a webserver, depending on their id:
webView.loadUrl("http://myserver/" + id + ".jpg");
Now it appears, that sometimes the images are larger than the device's screen width. That's why I added these lines:
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
The first 5 elements seem fitted perfectly, but when trying to swipe to the next fragment, you've got to first swipe the webview about 3px to the left, after that swipe again, to get to the next fragment. I don't know why this is happening , but after the initial 5 images, it works correctly. Maybe it has something to do with the fact that in the beginning I tell the pageradapter that there are 5 fragments to show, and after scrolling more fragments are added. These following fragments are not affected by this problem. This was the first problem.
The second problem is, when swiping through the completely correctly shown images, sometimes an image wil not be fitted to the screen. I've got to turn the screen to landscape and back to portrait, only then the image is fitted correctly. How can this happen, why does it work when the screen gets redrawn, but not on the first try? And it only appears to happen sometimes.
These are my two problems, the first one with the minimal wron fitting of the first 5 fragments and the second one the sometimes non-fitting images when swiping, but fit when screen gets redrawn
Here's my main activity:
public class MyActivity extends ActionBarActivity {
public static int NUM_PAGES = 5;
private ViewPager viewPager;
private CustomPagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
viewPager = (ViewPager)findViewById(R.id.activity_my_viewpager);
pagerAdapter = new CustomPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
if(NUM_PAGES - i <= 2) {
NUM_PAGES+=3;
}
pagerAdapter.notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
private class CustomPagerAdapter extends FragmentStatePagerAdapter {
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ScreenFragment.create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
And here's the fragment class:
public class ScreenFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int pageNumber;
public static ScreenFragment create(int pageNumber) {
ScreenFragment fragment = new ScreenFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNumber = getArguments().getInt(ARG_PAGE)+1;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.fragment_screen, container, false);
WebView webView = (WebView)viewGroup.findViewById(R.id.fragment_screen_webview);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("http://someserver/" + Integer.toString(number) + ".jpg");
return viewGroup;
}
}
Thanks in advance