Android interview series of 2018 articles section of Android ViewPager

Android interview series of 2018 articles section of Android ViewPager

ViewPager knowledge map

What 1.ViewPager that?

  ViewPager extended package is android v4 package class that allows users to switch the current left and right view.

  • 1) ViewPager class inherits directly ViewGroup class, all of which is a container class, can add another view class.
  • 2) ViewPager class needs a class PagerAdapter adapter provides data to it.
  • . 3) Fragment ViewPager often used together, and provides a special class FragmentPagerAdapter and FragmentStatePagerAdapter ViewPager used for the Fragment.

2.ViewPager adapter

  Used ListView or RecyclerView all know that it is similar to that displayed more Item controls, Android system are set to adapter mode, so ViewPager also need an adapter in order to display data, ViewPager adapter is PagerAdapter, it needs to implement four methods :

  • getCount (): Item number ViewPager displayed.
  • boolean isViewFromObject (View view, Object object ): Function: This function is used to determine instantiateItem (ViewGroup, int) back to the Key function with a page view is a view of the same representative of whether or not (i.e., whether the corresponding Talia, the corresponding expressed with a View)
    return value: If corresponds with a View, returns True, otherwise False.
  • Object instantiateItem (ViewGroup container, int position ): implementation of this function is to create a designated location page view. Adapters are responsible for the increase that will be created here View to view a given container, which is to ensure that when finishUpdate (viewGroup) returns the completed
    Return value: Object on behalf of a new view of the page
  • void destroyItem (ViewGroup container, int position, Object object): This function is implemented method to remove a given location page. Adapter has the responsibility to delete this view from the container. This is to ensure that the view can be removed when finishUpdate (viewGroup) return.

Example code:

public class AdapterViewpager extends PagerAdapter {
    private List<View> mViewList;

    public AdapterViewpager(List<View> mViewList) {
        this.mViewList = mViewList;
    }

    @Override
    public int getCount() {//必须实现
        return mViewList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {//必须实现
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {//必须实现,实例化
        container.addView(mViewList.get(position));
        return mViewList.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {//必须实现,销毁
        container.removeView(mViewList.get(position));
    }
}

Fragment of use combined with 3.ViewPager

3.1 Common adapter (FragmentPagerAdapter & FragmentStatePagerAdapter)

FragmentPagerAdapter
FragmentStatePagerAdapter

3.2 ViewPager in Fragment of lazy loading

https://blog.csdn.net/linglongxin24/article/details/53205878

3.3 ViewPager in Fragment life cycle analysis

https://blog.csdn.net/qq591920734/article/details/42490667

Production principle 4.Banner resolution map

https://blog.csdn.net/qq_22770457/article/details/51198688

Published 86 original articles · won praise 353 · views 240 000 +

Guess you like

Origin blog.csdn.net/ClAndEllen/article/details/82862804