gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:39:01 24 4
gpt4 key购买 nike

我试图在单击按钮时显示 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);
}
}

下面的代码是 fragment :

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);
}

}
}

任何想法,如何实现这一目标。

最佳答案

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

如果我想转到第 5 页,我使用了下面的代码

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

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

关于Android:单击按钮直接滚动/显示 ViewPager 中的特定页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18380987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com