问题描述
我有这个问题,我有一个带有 ViewPager
和一个 ViewPagerAdapter
的 FragmentActivity
.
I have this problem, I have a FragmentActivity
with a ViewPager
and a ViewPagerAdapter
.
在这个 ViewPager 中,我有 3 个 Fragments
,每个片段都包含一个 TableLayout
,它使用 2dArrayList
以编程方式填充.
In this ViewPager I have 3 Fragments
that each of them contains a TableLayout
that gets populated programmaticly using a 2dArrayList
.
单击 FragmentActivity
布局中的按钮,我过滤 2dArrayList
中的数据,基本上需要重新创建所有 3 个 Fragments
使用新数据集(在所有数据集中运行 onCreateView
方法).
on a click of a button in the FragmentActivity
layout, I filter the data in the 2dArrayList
and basically need to recreate all 3 Fragments
with the new dataset (run the onCreateView
method in all of them).
所以我为这个按钮创建了一个 onClickListener
:
So I create an onClickListener
for this button:
private class SlicerSelectedOnClickListener implements OnClickListener {
private ODSharedSlicer slicer;
public SlicerSelectedOnClickListener(ODSharedSlicer slicer) {
super();
this.slicer = slicer;
}
public void onClick(View v) {
String slicerValue = ((Button) v).getText().toString();
Log.d(TAG,"Slicer value: "+ slicerValue +" chosen");
application.currentReport.getODSharedSlicers().get(0).getSharedSlicerSelectedMemebers().add(slicerValue);
ViewGroup parent = (ViewGroup) ((Button) v).getParent();
for (int i = 0; i < parent.getChildCount(); i++)
{
if ((parent.getChildAt(i) != v) && (parent.getChildAt(i) instanceof ToggleButton))
{
((ToggleButton) parent.getChildAt(i)).setChecked(false);
}
}
mPagerAdapter.notifyDataSetChanged();
}
}
如您所见,我有一个 mPagerAdapter.notifyDataSetChanged();
如此处建议:
As you can see I have a mPagerAdapter.notifyDataSetChanged();
As suggested here:
如何故意用viewpager重新创建片段?
在此方法结束时,因此我希望适配器重新创建所有 Fragment
,但任何片段中的 onCreateView
都不会运行.
at the end of this method, So I expect the adapter to recreate all the Fragment
s, but the onCreateView
from any of the fragments never runs.
任何人都可以建议我做错了什么.任何帮助将不胜感激.
Can any one suggest what am I doing wrong. any assistance would be appreciated.
推荐答案
正如@Luksprog 在这里所建议的,将我的适配器类更改为:
As been suggested here by @Luksprog, changing my adapter class to this:
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private List<Fragment> fragments;
/**
* @param fm
* @param fragments
*/
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
解决了我的问题.
我所做的是添加这个方法:
what I did was to add this method:
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
这篇关于如何在 ViewPager 中重新创建所有片段:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!