需要一些帮助来解决我在使用 viewpager 时更新页面的问题.我正在使用带有 FragmentStatePagerAdapter 的简单 viewpager.我想要做的就是访问当前的片段/视图,以便我可以更新我的片段/视图中的一些文本视图.我在论坛里搜索了一下,知道的东西很少- 处理此问题的方法之一是在适配器的 instanceiteItem() 回调中设置标记并通过 findViewbyTag 检索视图.我无法理解如何实现这一点,我也不确定这是否适用于 FragmentStatePagerAdapter.- 我探索了各种论坛中建议的其他选项,但无法使它们发挥作用.
Need some help with my problem of updating pages while using viewpager. I am using a simple viewpager with FragmentStatePagerAdapter. All I want to do is get access to the current fragment/view so as I can update some textviews in my fragment/view. I searched around in the forum and came to know few things - One of the ways to handle this is by setting tag in instantiateItem() call back of the adapter and retreive the view by findViewbyTag. I could not understand how to implement this and I am also not sure if that will work for FragmentStatePagerAdapter. - I explored other options as suggested in various forums, but cannot make them work.
我的代码与 android http://developer.android.com/training 中的代码非常相似/animation/screen-slide.html.基本组件是相同的(带有一些显示组件的片段活动 xml,包括 textview、viewpager xml,其中只有一个视图分页器、一个 Fragment 类和主要的 FragmentActivity 类).在我的 FragmentActivity 类中,我向我的 viewpager 添加了一个 pageChangelistener,以便我可以在 onPageSelected() 期间更改我的 textview.
My code is very much same as in a android http://developer.android.com/training/animation/screen-slide.html. Basic components are the same (Fragment activity xml with some display components including a textview, viewpager xml with just a view pager in it, a Fragment class and the main FragmentActivity class). in my FragmentActivity class I have added a pageChangelistener to my viewpager so as I can do my textview changes during onPageSelected().
感谢任何帮助.
添加代码供参考.
Public class myActivity extends FragmentActivity
//Variable declarations
protected void onCreate(Bundle savedInstanceState) {
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
View CurrView;
OnPageChangeListener pageChangelistener = new OnPageChangeListener() {
@Override
public void onPageSelected(int pageSelected) {
doTextViewChnges();//access the text view and update it based on pageSelected
---THIS IS WHERE I AM STUCK IN TRYING TO GET THE TEXTVIEW IN MY CURRENT FRAGMWNT/VIEW-------
}
mPager.setOnPageChangeListener(pageChangelistener);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position, <other parameters I want to pass>);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
在你的 OnPageChangeListener 中:
in your OnPageChangeListener:
OnPageChangeListener pageChangelistener = new OnPageChangeListener() {
@Override
public void onPageSelected(int pageSelected) {
ScreenSlidePageFragment currentFragment = (ScreenSlidePageFragment) mPagerAdapter.getItem(pageSelected)
doTextViewChnges();//access the text view and update it based on pageSelected
---THIS IS WHERE I AM STUCK IN TRYING TO GET THE TEXTVIEW IN MY CURRENT FRAGMWNT/VIEW-------
}
这篇关于使用 fragmentStatePagerAdapter 更新 viewpager 中的片段/视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!