ViewPager 以编程方式设置当前页面

ViewPager set current page programmatically(ViewPager 以编程方式设置当前页面)

本文介绍了ViewPager 以编程方式设置当前页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个自定义 ViewPager 来禁用 Swipe Scroll,但我想以编程方式滑动.我的视图寻呼机中有三个 Tab,但是当我在第一个 Fragment 上调用 viewPager.setCurrentItem(viewPager.getCurrentItem()+1),它移动到第三个 Fragment 而不是第二个 Fragment.如果我在第二个 Fragment 中调用相同的函数,它会转到第三个.如果我在第三个片段中调用 (viewPager.getCurrentItem()-1)`,它可以通过向后移动来正常工作.任何帮助,将不胜感激.我的代码如下:

I wrote a custom ViewPager to disable Swipe Scroll, but I want to swipe programmatically. I have three Tab in my view pager, but when I call viewPager.setCurrentItem(viewPager.getCurrentItem()+1) on the first Fragment, it moves to the third Fragment instead of the second Fragment. And if I call same function in the second Fragment, it goes to the third. If I call (viewPager.getCurrentItem()-1)` in the third fragment, it works fine by moving back. Any help would be appreciated. My code is below:

NonSwipeAbleViewPager

public class NonSwipeableViewPager extends ViewPager {

private boolean swipeable;

public NonSwipeableViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyViewPager);
    try {
        swipeable = a.getBoolean(R.styleable.MyViewPager_swipeable, true);
    } finally {
        a.recycle();
    }
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    return swipeable ? super.onInterceptTouchEvent(event) : false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return swipeable ? super.onTouchEvent(event) : false;
}
}

XML 声明

<co.example.customview.NonSwipeableViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:swipeable="false" />

调用它

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menuNext:
            NonSwipeableViewPager pages = (NonSwipeableViewPager) getActivity().findViewById(R.id.pager);
            pages.setCurrentItem(pages.getCurrentItem()+1, true);
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return super.onOptionsItemSelected(item);
}

推荐答案

在你的PagerActivity中使用

In your PagerActivity use

    viewPager = (ViewPager) findViewById(R.id.view_pager);
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            int pagei = position + 1;
            pages=pagei + "";

            Toast.makeText(PagerActivity.this, getString(R.string.changeinfopage) + " " + pages, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        switch (item.getItemId()) {

            case R.id.action_previous:

                viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
                return true;

            case R.id.action_next:

                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                return true;
        }

        return super.onOptionsItemSelected(item);

   }

这篇关于ViewPager 以编程方式设置当前页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ViewPager 以编程方式设置当前页面

基础教程推荐