想要在 viewpager 页面上启动动画,但页面正在预加载

在我的项目中,我有几个屏幕(片段),我想为片段元素(视图)提供动画.我使用了视图分页器和视图分页器适配器和片段.我面临的主要问题 -

In my projects I have several screen(Fragments) and I want to give animation to the fragments elements(views). I have used view pager and viewpager adapter and fragments. The main problems I am facing-

  1. 在开始时正在加载 2 个页面,例如在开始时正在加载第 1 页和第 2 页.如果滑动到第二页,则它会自动加载第 3 页.
  2. 我需要显示使用 通用补间引擎.

代码有问题吗?

这是 MainActivity.java 代码-

    public class MainActivity extends FragmentActivity {    
    ViewPager viewPager;
    PagerAdapter adapter;
    CirclePageIndicator mIndicator;
    private int mWidthScreen;
    private int mHeightScreen;
    private Bundle bundle;
    private List<Fragment> frgScreens;
    private int selectedtheme;
    private Handler mHandler = new Handler();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_main); // Get the view from viewpager_main.xml        
        buildargument();        
        initialisePaging(); // Page creating function       
    }

    private void initialisePaging(int theme) {
        viewPager = (ViewPager) findViewById(R.id.pager);       
        frgScreens = new Vector<Fragment>(); // clearing old entries        
        viewPager.setOffscreenPageLimit(0);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                //Loading fragment # position

                //Instead of starting animation from onCreate start it from here 
                //Only for the first time (for page 1) this callback will not trigger
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        frgScreens = this.basicThemes();
        // Bind result to ViewPagerAdapter
        adapter = new ViewPagerAdapter(this.getSupportFragmentManager(), frgScreens);
        this.viewPager.setAdapter(adapter);
    }

    /*
     * Basic fragments
     */
    private List<Fragment> basicThemes() {
        // declaring fragments Group Basic
        Fragment frgBasic1 = Fragment.instantiate(this, Basic1.class.getName());
        Fragment frgBasic2 = Fragment.instantiate(this, Basic2.class.getName());
        Fragment frgBasic3 = Fragment.instantiate(this, Basic3.class.getName());
        Fragment frgBasic4 = Fragment.instantiate(this, Basic4.class.getName()); 
        Fragment frgBasic5 = Fragment.instantiate(this, Basic5.class.getName()); 

        // Passing arguments
        frgBasic1.setArguments(bundle);
        frgBasic2.setArguments(bundle);
        frgBasic3.setArguments(bundle);
        frgBasic4.setArguments(bundle); 
        frgBasic5.setArguments(bundle);



        // Loading screens into Fragment list
        List<Fragment> frgBasicScreens = new Vector<Fragment>();

        frgBasicScreens.add(frgBasic1);
        frgBasicScreens.add(frgBasic2);
        frgBasicScreens.add(frgBasic3);
        frgBasicScreens.add(frgBasic4); 
        frgBasicScreens.add(frgBasic5);

    return frgBasicScreens;

}

private void getScreenHeightWidhth() {
    DisplayMetrics localDisplayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
    this.mWidthScreen = localDisplayMetrics.widthPixels;
    this.mHeightScreen = localDisplayMetrics.heightPixels;
    mHandler.post(
            new DisplayToast(this, "Width Screen:" + mWidthScreen + ". Height Screen: " + mHeightScreen + "."));
}

private void buildargument() {
    this.bundle = new Bundle();
    bundle.putInt("mWidthScreen", this.mWidthScreen);
}   

@Override
protected void onResume() {
    super.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
}}

现在ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter {    
private List<Fragment> screens; 

public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments ) {
    super(fm);      
    this.screens = fragments;       
}

@Override
public Fragment getItem(int pos) {
    // getting the position of the item
    return this.screens.get(pos);       
}

@Override
public int getCount() {
    // Getting the size of screen passed
    return this.screens.size();
}}

skinbase.java是所有基础屏一些共享方法使用的通用类.

skinbase.java is the common class used by all the basics screen some shared methods.

public abstract class SkinsBase extends Fragment
{
 protected int mWidthScreen;
 protected int mHeightScreen;
 public int skinHeight;
 public int skinWidth;
 public final int textShadowSize = 2;
 .................
 }

现在basic1.java

public class Basic1 extends SkinsBase { 
private RelativeLayout mBlueBackground;
private Handler mHandler = new Handler();
private static final String TAG = "Basic1";

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }       
    mContext = container.getContext();
    View v = inflater.inflate(R.layout.blank_viewpager_item, container, false);
    mLayoutForeground = (RelativeLayout) v.findViewById(R.id.relative);

    mBlueBackground = initshape(getResources().getColor(R.color.transparent_blue_80),this.mWidthScreen, (int) (0.10F * this.mWidthScreen), 0,(int) (0.12F *this.mWidthScreen));     
    this.mLayoutForeground.addView(this.mBlueBackground);   

    Log.e(TAG, "Inside basic 1 ");
    return v;
}



public RelativeLayout initshape(int intColor,int intWidthscreen, int intHeight, int intMerginLeft,int intMerginTop) {
    RelativeLayout localRelativeLayout = new RelativeLayout(this.mContext);             
    localRelativeLayout.setBackgroundColor(intColor);       
    RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(intWidthscreen, intHeight);
    localLayoutParams.setMargins(intMerginLeft, intMerginTop, 0, 0);
    localRelativeLayout.setLayoutParams(localLayoutParams);         
    return localRelativeLayout;
}   

public void onResume() {
    super.onResume();
}
@Override
public void onPause() {
    super.onPause();
}}

其他basic2.java、basic3.java、basic4.java.....与basic1.java类似

我需要将动画应用到 RelativeLayout.是否可以?而且我希望在页面加载之前开始动画.

I need to apply animation to RelativeLayout. Is it possible? And I want the start of the animation when the page loads not before that.

推荐答案

view-pager 的默认行为是从当前页面的两侧预加载至少 1 个页面.见以下链接

It is the default behavior of view-pager that it preload at-least 1 page from both side of current page. See following links

ViewPager.setOffscreenPageLimit(0) 无法按预期工作

所以你可以做的不是启动动画 onCreate 从 onPageScrolled 回调启动它

So what you can do is Instead of starting animation onCreate start it from onPageScrolled callback

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        //Loading fragment # position

        //Instead of starting animation from onCreate start it from here 
        //Only for the first time (for page 1) this callback will not trigger
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});

参见 setOffscreenPageLimit

设置应保留到两侧的页数视图层次结构中的当前页面处于空闲状态.超出此范围的页面需要时会从适配器重新创建限制.

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

这是作为优化提供的.如果你事先知道号码您需要支持或具有延迟加载机制的页面放置在您的页面上,调整此设置可以在以下方面受益分页动画和交互的感知平滑度.如果你有您可以一次保持活动的少量页面(3-4),更少的时间将花费在新创建的视图子树的布局上用户页面来回.

This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

您应该将此限制保持在较低水平,尤其是当您的网页内容复杂时布局.此设置默认为 1.

You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.

相关文章