Android:在按钮点击时直接滚动/显示ViewPager中的特定页面

时间:2013-08-22 12:50:03

标签: android android-fragments android-viewpager android-fragmentactivity fragmentpageradapter

我试图在按钮点击时显示ViewPager的特定页面。它正在工作,但滚动到该特定页面并不顺利,即假设我在页面号。 1但是如果我想去第5,其他页面2,3,4也滚动。我只想滚动到该特定页面。怎么做。我使用了以下代码,请看一下:

下面的代码是FragmentActivity:

public class ScreenSlideFragmentActivity extends FragmentActivity implements SlidingPageInterface{
    private static final int NUM_PAGES = 6;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_slide);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
            }
        });
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            return ScreenSlidePageFragment.create(position, NUM_PAGES, ScreenSlideFragmentActivity.this);
        }

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

    @Override
    public void notifyToSlideToRespectivePage(int pageNumber) {
        mPager.setCurrentItem(pageNumber, true);
    }
}

下面的代码是片段:

public class ScreenSlidePageFragment extends android.support.v4.app.Fragment {
        public static final String ARG_CURRENT_PAGE = "current_page";
        public static final String ARG_TOTAL_PAGE_COUNT = "total_page_count";
        private int mPageNumber;
        private int mTotalPageCount;
        private static ScreenSlideFragmentActivity mActivity;
        public static ScreenSlidePageFragment create(int pageNumber, int totalNoPages, ScreenSlideFragmentActivity activity) {
            Log.e("create", "create called");
            ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_CURRENT_PAGE, pageNumber);
            args.putInt(ARG_TOTAL_PAGE_COUNT, totalNoPages);
            fragment.setArguments(args);
            mActivity = activity;
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mPageNumber = getArguments().getInt(ARG_CURRENT_PAGE);
            mTotalPageCount = getArguments().getInt(ARG_TOTAL_PAGE_COUNT);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {        
            ViewGroup rootView = (ViewGroup) inflater
                    .inflate(R.layout.fragment_screen_slide_page, container, false);

            // Write Logic below to Dynamically Display Buttons as per "total_page_count"

            return rootView;
        }
        public int getPageNumber() {
            return mPageNumber;
        }

        public class HandleDotIVClickListener implements View.OnClickListener {
            @Override
            public void onClick(View view) {            
                int tag = (Integer) view.getTag();
                mActivity.notifyToSlideToRespectivePage(tag);
            }

        }
    }

任何想法,如何实现这一点。

3 个答案:

答案 0 :(得分:6)

我有一个应用程序,其中有超过100页,以及一个索引页面,它导致特定的页码。从索引页面,如果我试图转到20页,我可以看到屏幕上的闪烁,从1到19再到20。

如果我想进入第5页

,我使用了以下内容
viewPager.setCurrentItem(4,false);
viewPager.setCurrentItem(5);

这使得滚动停止并进入特定页面而不显示其他页面并且屏幕上没有闪烁。

答案 1 :(得分:0)

据我所知,您不能只在不滚动浏览其他页面的情况下将ViewPager滚动到特定页面。

如果要使用自定义转换来更改片段,则应使用FragmentTransaction.setCustomAnimations(int i, int i1, int i2, int i3)

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

Fragment newFragment = Fragment.newInstance();
ft.setCustomAnimations(R.anim.fragment_slide_left_enter,
                       R.anim.fragment_slide_left_exit,
                       R.anim.fragment_slide_right_enter,
                       R.anim.fragment_slide_right_exit);
ft.replace(R.id.main_fragment_container, newFragment);
ft.addToBackStack(null);
ft.commit();

片段输入:

fragment_slide_left_enter.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@anim/decelerate_quint">
    <translate android:fromXDelta="33%" android:toXDelta="0%p"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

片段退出:

fragment_slide_left_exit.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@anim/decelerate_quint">
    <translate android:fromXDelta="0%" android:toXDelta="-33%p"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

答案 2 :(得分:0)

使用(manager.beginTransaction().hide(f).commitNow();)隐藏其他页面。

然后在完成动画使用(manager.beginTransaction().show(f).commitNow();

之后

我在CustomPagerAdapter中的代码

for(Fragment f : fragmentList)
{
    if(!current.equals(f) && !newF.equals(f))
        {
            manager.beginTransaction().hide(f).commitNow();
        }
}

viewPager.setCurrentItem(index, byAnimation);