我正在尝试在单击按钮时显示 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);
}
}
}
任何想法,如何实现这一点。