Directory:

1. Disable the transition animation that the ViewPager scrolls when clicking on a tag. 2, Solve the problem that the Adapter notifyDataSetChanged() is invalid. 3. Set the number of preloaded pages in ViewPager. 4. Disable the left and right switching of ViewPager gestures. 5, ViewPager left and right sliding inertia disappeared bug. 6. The ViewPager communicates with the child View.


1. Disable the ViewPager transition animation when clicking on a tag:

When a TabLayout control is attached to a ViewPager, you can click a TAB to switch the ViewPager. But what if you don’t want the animation to switch? ** Solution: ** Is actually very simple, the key code is a sentence:

// The second argument is to disable the scrolling transition effect
mViewPager.setCurrentItem(0.false);
Copy the code

Adapter notifyDataSetChanged() is invalid

When you call the Adapter notifyDateSetChanged() to update the ViewPager data, you will find that there is no effect. If you slide two pages to the third page, you will find that the data is not refreshed except for the three pages cached by the default ViewPager. The following data (after the third page) is refreshed. If you return to the first page, you will find that the data on the first page has changed. ** Copy the Adapter’s getItemPosition method and return POSITION_NONE.

public int getItemPosition(Object object) {    
  return POSITION_NONE;
}
Copy the code

Existing problems: This is not a Bug in PagerAdapter. Normally, calling the notifyDataSetChanged method causes the ViewPager to query all child Views once through the getItemPosition method of the Adapter. In this case, If all Child Views are POSITION_NONE, the ViewPager will call destroyItem to destroy and rebuild them, adding overhead and causing logic problems in complex cases. Especially when you just want to update the content of the Child View, this creates completely unnecessary overhead. A more effective way: A more reliable approach is to implement notifyDataSetChanged based on your requirements, such as adding flags on instantiateItem() with the View.setTag method and updating information on instantiateItem(). Find the corresponding View and update it with findViewWithTag.


Set the number of preloaded pages in ViewPager:

ViewPager preloads the left and right pages by default. If you want to preload more, simply call ViewPager’s setOffscreenPageLimit().

mViewPager.setOffscreenPageLimit(2); // Set the number of views cached (actually 3, 2 + 1 being displayed)
Copy the code

4. Disable the left and right switching operation of ViewPager gesture:

** Custom View inherits from ViewPager with the following code:

public class ViewPagerEx extends ViewPager{
    private boolean isPagingEnabled = true;

    public ViewPagerEx(Context context) {
        super(context);
    }
    public ViewPagerEx(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

    public void setPagingEnabled(boolean canScroll) {
        this.isPagingEnabled = canScroll;
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        super.setCurrentItem(item, smoothScroll); }}Copy the code

Usage:

mViewPager.setPagingEnabled(false);// Disable left/right sliding
mViewPager.setPagingEnabled(true);// Enable left/right sliding
Copy the code

5, ViewPager left and right sliding inertia disappeared bug.

Does the FragmentPagerAdapter constructor pass getFragmentManager()? If so, change to getChildFragmentManager(), and yes, it’s that simple.


6. The ViewPager communicates with the child View.

For example, I want to notify the child View and refresh it every time the ViewPager swipe is done.

// Use the Tag to get the corresponding child View, and then operate on the View.
 viewPager.findViewWithTag(position);
// Set the child View Tag in Adapter's instantiateItem method
view.setTag(position);
Copy the code

(Update from time to time)