4

我正在尝试在单击按钮时显示 ViewPager 的特定页面。它正在工作,但滚动到该特定页面并不顺畅,即假设我在第 1 页。1 但如果我想转到第 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);
            }

        }
    }

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

4

3 回答 3

7

我有一个超过 100 页的应用程序,以及一个指向特定页码的索引页。如果我尝试转到第 20 页,则从索引页面,我可以看到屏幕闪烁,从 1 -19 到 20。

如果我想转到第 5 页,我使用了以下内容

viewPager.setCurrentItem(4,false);
viewPager.setCurrentItem(5);

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

于 2013-10-16T12:46:16.220 回答
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>
于 2013-08-22T13:38:36.003 回答
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);
于 2019-05-14T13:20:03.623 回答