Android ViewPager滚动滞后

时间:2018-05-03 09:40:00

标签: android android-viewpager

我的应用程序中有一个ViewPager滚动3个片段。它的问题是滚动是滞后的。首先我认为是因为我更改了onPageSelected方法中的指标。但后来发现这不是问题.Below是代码..等待你的帮助..

代码: ViewpagerActivity.java:

binding.viewpager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
        binding.viewpager.setCurrentItem(0);

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

            @Override
            public void onPageSelected(int position) {
  }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

PagerAdapter.java:

public class ViewPagerAdapter extends FragmentPagerAdapter
    {

        ArrayList instances;


        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
            instances=new ArrayList();
            instances.add(new Frag1());
            instances.add(new Frag2());
            instances.add(new Frag3());
        }

        @Override
        public int getCount() {
            if (instances!=null) {
                return instances.size();
            }
            else {
                return 0;
            }
        }

        @Override
        public Fragment getItem(int position) {
            Toast.makeText(getApplicationContext(),Integer.toString(position),Toast.LENGTH_SHORT).show();
            return (Fragment) instances.get(position);
        }

//        @Override
//        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
//            return false;
//        }
    }

Frag1.java:

public class Frag1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.frag1,container,false);
        return view;

    }
}

Frag2.java:

public class Frag2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.frag2,container,false);
        return view;
    }
}

Frag3.java:

public class Frag3 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.frag3,container,false);
        return view;
    }
}

0 个答案:

没有答案