In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article, "what is the working principle of ViewPager for Android development", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "what is the working principle of ViewPager in Android development?"
In APP development, the ViewPager and PagerAdapter classes do a lot of work for us in the background. So how does ViewPager work? Before we introduce you to how ViewPager works, what we need to know is that in most cases, we don't need to know the details of its internal implementation. However, if you want to implement the PagerAdapter interface yourself, you need to understand the similarities and differences between ViewPager-PagerAdapter and Recycler-View-Adapter.
So when do you need to implement the PagerAdapter interface yourself? If you need ViewPager to host non-fragment views, you need to implement the native PagerAdapter interface. For example, when hosting a common view object such as a picture in ViewPager.
Speaking of which, I believe you have questions again, why choose to use ViewPager instead of RecyclerView? Don't worry, the editor of Nanchang APP production and Development Company will tell you:
That's because you can't use existing Fragment, so using RecyclerView in CriminalIntent applications requires a lot of internal implementation work. Adapter requires us to provide View in a timely manner. However, it is FragmentManager that determines when the fragment view is created. Therefore, when RecyclerView asks Adapter to provide a fragment view, we cannot immediately create a fragment and provide its view.
This is why ViewPager exists. It uses the PagerAdapter class instead of the original Adapter. PagerAdapter is much more complex than Adapter because it handles more view management work. The following is its basic internal implementation.
PagerAdapter does not use onBindViewHolder (...) that returns views. Method, but use the following methods:
Public Object instantiateItem (ViewGroup container, int position)
Public void destroyItem (ViewGroup container, int position, Object object)
Public abstract boolean isViewFromObject (View view, Object object)
The PagerAdapter.instantiateItem (ViewGroup, int) method tells pager adapter to create a list item view at the specified location and then adds it to the ViewGroup view container, while the destroyItem (ViewGroup, int,Object) method tells pager adapter to destroy the built view. Note that the instantiateItem (ViewGroup, int) method does not require that the view be created immediately. Therefore, it is up to PagerAdapter to decide when to create views.
After the view is created, ViewPager will notice it at some point in time. To determine which object the view belongs to, ViewPager calls the isViewFromObject (View, Object) method. Here, the Object parameter is the object returned by the instantiateItem (ViewGroup,int) method. Therefore, assuming that ViewPager calls the instantiateItem (ViewGroup, 5) method to return an An object, the isViewFromObject (View, A) method should return a true value, otherwise it should return a false value, as long as the passed View parameter is the view of the fifth object.
For ViewPager, this is a complex process, but for PagerAdapter, it is nothing. Because PagerAdapter only needs to be able to create, destroy, and identify which object the view comes from. This requirement is obviously lenient, so PagerAdapter has the freedom to create and add a new fragment through the instantiateItem (ViewGroup, int) method, and then return an Object (fragment) that can be tracked and managed. The following is the specific implementation of the isViewFromObject (View, Object) method:
@ Override
Public boolean isViewFromObject (View view, Object object) {
Return ((Fragment) object) .getView () = = view
}
As you can see, it's a real ordeal to override these methods of implementing PagerAdapter every time you need to use ViewPager. Fortunately, we have FragmentPagerAdapter and FragmentStatePagerAdapter convenience classes, so we won't be in so much trouble!
The above is about "what is the working principle of ViewPager developed by Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.