From:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0804/1504.html
I use Viewpager to do a sliding switch effect, the effect is quite good, concrete implementation of the effect on the Internet a lot, you can go to find, but after the implementation of the switch effect, because the needs, I need to get the current display of the view, which is very disgusting. Viewpager has Setonpagechangelistener, there are
@Override
Public voidonpageselected (int arg0) {
}
@Override
Public voidonpagescrolled (int arg0, float arg1, int arg2) {
}
@Override
Public voidonpagescrollstatechanged (int arg0) {
}
Method that I have started is in the Onpageselected method through Viewpager.getchildat (arg0) mode and want to get the currently displayed view. However, after more than 3 view will be reported null pointer error, the obtained view will be empty, tracking found arg0 greater than 2 after the view is empty. So I guess viewpager there are up to 3 view, more than three times the old recycling. So the current display, in addition to the first and last, should be the middle one, that is, the arg0 1. So I judge, except the first and the last, the other arg0 are equal to 1, found no error, but the view is not correct. Later through the Getchildcount () method found that ChildCount sometimes 2, sometimes 3, which makes me very headache.
Originally in Viewpager's adapter there is a way to get to the current view, as follows: [Java] View Plain copy private view mcurrentview; @Override Public void setprimaryitem (Viewgroup container, int position, object object ) { mCurrentView = (View) object; }    public view getprimaryitem () { return mCurrentView; }
Viewpager Adapter Sets the current display child view by Setprimaryitem method.
Saving it can be followed by a custom getprimaryitem to get to the current child view.