如何通过Viewpager的setCurrentItem方法使箭头不可见?

时间:2015-08-25 02:25:48

标签: android

在我的MainActivity的Oncreate中,我添加了以下代码:

describe("blah", function() {
    it("f sets store.p1", function() {
        var fixture2expectation = {
            0: "Some Text",
            1: "Some Other Text",
            2: "Another Text"
        };

        for (var fixtureString in fixture2expectation){
            var fixture = parseInt(fixtureString);
            var expectation = fixture2expectation[fixture];
            var mockApiCall = function (){return fixture;};
            var mockStore = {};
            f(mockApiCall, mockStore);
            expect(mockStore.p1).toBe(expectation);
        }
    });
});

并点击按钮我添加此代码:

mDateAreaSlider.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {

                    // Hide right arrow if reach last position 
                    if (arg0 == adapter.DateList.length - 1) {
                        mButtonNext.setVisibility(View.INVISIBLE);
                        mButtonBack.setVisibility(View.VISIBLE);
                    }

                    // Hide left arrow if reach first position 
                    else if (arg0 == 0) {
                        mButtonBack.setVisibility(View.INVISIBLE);
                        mButtonNext.setVisibility(View.VISIBLE);
                    }

                    // Else show both arrows
                    else {
                        mButtonBack.setVisibility(View.VISIBLE);
                        mButtonNext.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
                ifSingding = true;
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
            }
        });

但问题是它可以滑动到我选择的页面但不会使我的箭头不可见。

注意: 2个箭头是主要布局中的ImageViews,而不是充气布局。所以它与适配器无关。

1 个答案:

答案 0 :(得分:2)

用以下代码替换原始代码:

final ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == adapter.DateList.length - 1) {
                    mButtonNext.setVisibility(View.INVISIBLE);
                    mButtonBack.setVisibility(View.VISIBLE);
                }

                // Hide left arrow if reach first position 
                else if (position == 0) {
                    mButtonBack.setVisibility(View.INVISIBLE);
                    mButtonNext.setVisibility(View.VISIBLE);
                }

                // Else show both arrows
                else {
                    mButtonBack.setVisibility(View.VISIBLE);
                    mButtonNext.setVisibility(View.VISIBLE);
                }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
};
mDateAreaSlider.addOnPageChangeListener(onPageChangeListener);

点击按钮

执行此代码
mDateAreaSlider.setCurrentItem(0);
mDateAreaSlider.post(new Runnable() {
        @Override
        public void run() {
            onPageChangeListener.onPageSelected(0);
        }
});