Viewpager how to get the current sub-view (resolves getchildat (position) null problem)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.