让InfiniteViewPager工作

时间:2016-01-20 05:38:40

标签: java android android-viewpager android-pageradapter

我知道有一些关于无限分页的问题,但我仍然难以让它们中的任何一个工作。 (我在Android Studio工作)

我正试图从https://github.com/antonyt/InfiniteViewPager开始运行InfiniteViewPager。

我正在努力使其工作,以便我可以划过4页/片段(A,B,C,D),右侧滑动功能D返回A,左侧滑动A返回D。

我已导入InfinitePagerAdapter和InfiniteViewPager Java类,并将InfiniteViewPager添加到我的activity_main.xml文件中。

然而,我仍然坚持我需要的地方"用InfinitePagerAdapter包裹现有的PagerAdapter"。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    PagerAdapter mPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mPagerAdapter);
        //Cannot resolve symbol adapter
        PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);
        mViewPager.setAdapter(wrappedAdapter);

        mViewPager.setOffscreenPageLimit(5);
    }

    public class PagerAdapter extends FragmentStatePagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            //getItem is called to instantiate the fragment for the given page.
            switch (position) {

                default:
                    return Fragment1.newInstance();
                case 1:
                    return Fragment2.newInstance();
                case 2:
                    return Fragment3.newInstance();
                case 3:
                    return Fragment4.newInstance();
            }
        }

        @Override
        public int getCount() {
            // Show number of total pages.
            return 4;
        }
    }
}

我不完全确定应该用adapter代替def getBestWeightsByRandomGradientAscent(featureDatasList, classTypes, maxCycles=1): """ :param featureDatasList: :param classTypes: :param maxCycles: the loop time :return: """ import random featureDatas = np.array(featureDatasList) m, n = np.shape(featureDatas) weights = np.ones(n) # the loop goes here... # for j in range(maxCycles): featureIndexs = range(m) featureLen = len(featureIndexs) for i in range(m): delta = 4 / (1.0 + i + j) + 0.01 randIndex = int(random.uniform(0, featureLen)) sigmodInput = sum(featureDatas[randIndex] * weights) estimateClass = calculateSigmodEstimateClassType(sigmodInput) error = classTypes[randIndex] - estimateClass weights += (error * delta) * featureDatas[randIndex] del (featureIndexs[randIndex]) return weights ,或者是否应该从其他Java类中删除它。另外还有什么我需要添加/更改以使其工作?

1 个答案:

答案 0 :(得分:0)

在对演示进行了一些修补之后我才开始工作 - 代码如下:

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        PagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

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

            @Override
            public Fragment getItem(int position) {
                final ArrayList<Fragment> fragmentPosition = new ArrayList<>();
                fragmentPosition.add(new Fragment1());
                fragmentPosition.add(new Fragment2());
                fragmentPosition.add(new Fragment3());
                fragmentPosition.add(new Fragment4());
                Fragment fragment = fragmentPosition.get(position);
                Bundle args = new Bundle();
                fragment.setArguments(args);
                return fragment;
            }
        };

        // wrap pager to provide infinite paging with wrap-around
        PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);

        // actually an InfiniteViewPager
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(wrappedAdapter);

    }
}