圆形 ViewPager.片段在第一轮后无法正常工作

Circular ViewPager. Fragments don#39;t work as they supposed to after first round(圆形 ViewPager.片段在第一轮后无法正常工作)

本文介绍了圆形 ViewPager.片段在第一轮后无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我需要循环 ViewPager.我很难实施它.现在我已经实现了它并且就圆形滚动而言它工作正常.但我注意到一个问题.即,第一轮滚动片段后不起作用.我有三个片段,里面有一个按钮.按钮在第一轮工作,但当我回到第一页按钮不起作用.p.s 我使用假页面技术使 viewpager 循环.

Ok, so i needed circular ViewPager. I was having really hard time to implement it. Now that i have implemented it and it is working fine as far as circular scroll is concern. But i have notice a problem.Which is, after first round of scroll fragments don't work. I have three Fragments with a button inside them. Button works in first round but when i come back to first page button doesn't work. p.s i used fake pages technique to make viewpager circular.

这里是代码

        mViewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), this));
    //mViewPager.setOnPageChangeListener(new CircularViewPagerHandler(mViewPager));
    mViewPager.setCurrentItem(1, false);
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                mViewPager.setCurrentItem(3, false);
                Log.d("TAG",
                        "Swiped before first page, looping and resetting to last page.");
            } else if (position == 4) {
                mViewPager.setCurrentItem(1, false);
                Log.d("TAG",
                        "Swiped beyond last page, looping and resetting to first page.");
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            Log.d("onPageScrollState", "state:" + state);
            int currentPage = mViewPager.getCurrentItem();
            if(currentPage==1){
                imgCenter.setImageResource(R.drawable.take_ride);
                imgLeft.setImageResource(R.drawable.give_ride);
                imgRight.setImageResource(R.drawable.setting);
            }
            if(currentPage==2){
                imgCenter.setImageResource(R.drawable.setting);
                imgRight.setImageResource(R.drawable.take_ride);
                imgLeft.setImageResource(R.drawable.give_ride);
            }
            if (currentPage==3){
                imgCenter.setImageResource(R.drawable.give_ride);
                imgLeft.setImageResource(R.drawable.take_ride);
                imgRight.setImageResource(R.drawable.setting);

            }
        }
    });

这里是 PagerAdapter 类

here is PagerAdapter class

  public class PagerAdapter extends FragmentPagerAdapter {

Context mcontext;

public PagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    mcontext = context;
}

@Override
public Fragment getItem(int position) {
    if (position == 0) {
        return new HomeFragment2();
    }
    else if(position==1){
        return new HomeFragment();
    }
    else if (position == 2) {
        return new SettingFragment();
    }
    else if(position==3){
        return new HomeFragment2();
    }
    else
        return new HomeFragment();
}

@Override
public int getCount() {
    return 5;
}
}

推荐答案

我和你一起创建了一个简单的测试应用 PagerAdapter 和简单的 Fragments代码>按钮:

I've created a simple test-app with you PagerAdapter and simple Fragments with a Button:

而且效果很好!

我已经上传了源代码这里,所以你可以检查一下,看看和你的有什么不同.

I've uploaded the source code here, so you can check it out and see if there's any difference with yours.

作为 Fragment 的我使用:

public class FragmentA extends android.support.v4.app.Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_a, container, false);
        rootView.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Hi, fragment A", Snackbar.LENGTH_SHORT).show();
            }
        });
        return rootView;
    }
}

那么我可以推荐什么

  • 用一些虚拟的片段替换你的片段(如上面的那个).如果它有效 - 问题出在 Fragment 代码中,而不是在 Circular ViewPager

  • replace your fragments with some dummy ones (like the one above). If it works - the issue is in the Fragment code, not in the Circular ViewPager

如果它仍然不起作用 - 我会尝试而不是多次重新创建片段 - 创建一次并存储:

If it still doesn't work - I'd try to instead of re-creating fragments multiple times - create them once and store:

 public class PagerAdapter extends FragmentPagerAdapter {
    Context mcontext;
    Fragment [] fragments;
    public PagerAdapter(FragmentManager fm, Context context, Fragment [] fragments) {
        super(fm);
        mcontext = context;
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments[position];
    }

    @Override
    public int getCount() {
        return fragments.length;
    }
}

而在Activity中:

Fragment[] fragments = {
        Fragment.instantiate(this, FragmentC.class.getName()),
        Fragment.instantiate(this, FragmentA.class.getName()),
        Fragment.instantiate(this, FragmentB.class.getName()),
        Fragment.instantiate(this, FragmentC.class.getName()),
        Fragment.instantiate(this, FragmentA.class.getName()),
};
.....
viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), this, fragments));

如果有帮助,请告诉我!

Let me know, if it helps!

这篇关于圆形 ViewPager.片段在第一轮后无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:圆形 ViewPager.片段在第一轮后无法正常工作

基础教程推荐