在ViewPager更新片段 [英] updating Fragments in a ViewPager
问题描述
我有试图更新/在ViewPager更换碎片问题。问题是旧的片段根本不会得到由新的替换。我看过一对夫妇计算器的解决方案,并尝试过,但它仍然无法正常工作。任何人有任何想法,为什么?我会解释什么,我想下面的事情。
i am having problems trying to update/replace Fragments in a ViewPager. the problem is that the old fragments simply won't get replaced by the new ones. i have read a couple of solutions on stackoverflow, and tried them, but it still doesn't work. anyone have any ideas why? i will explain what i am trying to do below.
我有一个POJO类
public class Pojo {
public String text;
public Pojo(String text) { this.text = text; }
}
我有一组类,PojoGroup
i have a group class, PojoGroup
public class PojoGroup {
public String title;
public List<Pojo> pojos;
public PojoGroup(String title, List<Pojo> pojos) {
this.title = title;
this.pojos = pojos;
}
}
的想法是,我将在PojoGroup绑定到一个片段。该片段的XML,pojogroup_frag.xml,看起来像下面这样。你可以看到,我将设置为tvTitle和PojoGroup.title POJO的列表绑定到ListView。
the idea is that i will bind the PojoGroup to a Fragment. the XML of the fragment, pojogroup_frag.xml, looks like the following. as you can see, i will set the tvTitle to PojoGroup.title and bind the list of Pojo to the ListView.
<LinearLayout xmlns:android="..."
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/tvTitle" android:layout_width="..." android_layout_height="..."/>
<ListView android:id="@+id/listView" android:layout_width="..." android_layout_height="..."/>
</LinearLayout>
有关完整,这里是我的POJO列表绑定到ListView POJO适配器。
for completeness, here is my pojo adapter to bind the list of pojos to the ListView.
public class PojoAdapter extends ArrayAdapter<Pojo> {
public PojoAdapter(Context ctx, int resId, List<Pojo> objects) {
super(ctx, resId, objects);
}
public View getView(int pos, View convertView, ViewGroup parent) {
View v = convertView;
if(null == v) {
Context ctx = getContext();
LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflat(R.layout.pojo_row, null);
}
TextView tv = (TextView)v.findViewById(R.id.tvText);
Pojo pojo = getItem(pos);
tv.setText(pojo.text);
}
}
我pojo_row.xml文件看起来像下面这样。
my pojo_row.xml file looks like the following.
<LinearLayout xmlns:android="..."
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/tvText" android:layout_width="..." android_layout_height="..."/>
</LinearLayout>
我main.xml中有一个按钮和一个ViewPager。我MainActivity类别看起来像下面这样。
my main.xml has a Button and a ViewPager. my MainActivity class looks like the following.
public class MainActivity extends FragmentActivity {
PojoGroupPagerAdapter pagerAdapter;
ViewPager viewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.bReplace);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { generateAndReplaceFrags(); }
});
pagerAdapter = new PojoGroupPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager)findViewById(R.id.pager);
viewPager.setAdapter(pagerAdapter);
}
void generateAndReplaceFrags() {
//PojoGroupFactory just generates a random List of PojoGroups
List<PojoGroup> groups = PojoGroupFactory.getPojoGroups();
pagerAdater.setPojoGroups(groups);
}
}
我的POJO片段,PojoFrag,看起来像下面这样。
my pojo fragment, PojoFrag, looks like the following.
public class PojoFrag extends Fragment {
PojoGroup pojoGroup;
View view;
TextView tvTitle;
ListView listView;
public PojoFrag(PojoGroup group) { pojoGroup = group; }
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.pojogroup_frag, null);
listView = (ListView)view.findViewById(R.id.listView);
tvTitle = (TextView)view.findViewById(R.id.tvTitle);
List<Pojo> objects = pojoGroup.getPojos();
listView.setAdapter(new PojoAdapter(getActivity(), R.id.tvText, objects));
tvTitle.setText(pojoGroup.title);
return view;
}
}
在PojoGroupPagerAdapter类不是太复杂了,这个类就是一些对计算器previously建议修改的建议进行。但是,它仍然没有工作。如果需要,我可以提供完整的项目源$ C $ C。
the PojoGroupPagerAdapter class is not too complex, and this class is where some of the suggestions on stackoverflow previously suggested modifications be made. however, it is still not working. i can provide the full project source code if needed.
public class PojoGroupPagerAdapter extends FragmentPagerAdapter {
List<PojoGroup> pojoGroups;
Map<Integer, Fragment> fragments;
public PojoGroupPagerAdapter(FragmentManager fm) {
super(fm);
fragments = new HashMap<Integer, Fragment>();
}
public Fragment getItem(int position) {
Integer key = new Integer(position);
if(fragments.containsKey(key)) {
return fragments.get(key);
}
PojoGroup group = pojoGroups.get(position);
PojoFrag frag = new PojoFrag(group);
fragments.put(key, frag);
return frag;
}
public int getCount() {
return (null == pojoGroups) ? 0 : pojoGroups.size();
}
public int getItemPosition(Object object) {
if(!fragments.containsKey(object))
return POSITION_NONE;
return POSITION_UNCHANGED;
}
public void setPojoGroups(List<PojoGroup> pojoGroups) {
this.pojoGroups = pojoGroups;
fragments.clear();
int total = pojoGroups.size();
for(int i=0; i < total; i++) {
Integer key = new Integer(i);
PojoGroup group = pojoGroups.get(i);
PojoFrag frag = new PojoFrag(group);
fragments.put(key, frag);
}
notifyDataSetChanged();
}
}
你可以看到,我已经覆盖了getItemPosition(Object)方法,但是这仍然没有帮助,以取代用新的旧观点。我也打得四处FragmentTransaction,但这也不管用。我试图创建一个新的ViewPager对象,并将其添加到主视图。但是,我得到一些奇怪的异常(可能是因为我不知道如何来构建的AttributeSet)。
as you can see, i have overridden the getItemPosition(Object) method, but this still does not help to replace the old view with the new one. i also played around with FragmentTransaction, but this doesn't work either. i am attempting to create a new ViewPager object and add it to the main view. however, i am getting some weird exception (probably because i have no idea how to construct AttributeSet).
这个问题的任何帮助AP preciated。
any help with this problem is appreciated.
是链接到演示项目: http://www.box.com/s/xbtm3amtkj7f13j2llm4
推荐答案
好吧,让我们看看我FragmentPagerAdapter
ok let see my FragmentPagerAdapter
public class StudentPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> listFragments;
public StudentPagerAdapter(FragmentManager fm, List<Fragment> _listFragments) {
super(fm);
this.listFragments = _listFragments;
}
@Override
public Fragment getItem(int position) {
return listFragments.get(position);
}
@Override
public int getCount() {
return listFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return "Page " + (position + 1);
}
}
该ViewPager有viewPagerAdapter在这种情况下,它是你的PojoGroupPagerAdapter类
PojoGroupPagerAdapter有很多片段(这是你的PojoFrag类),viewPager显示每次1片段。
所以当单击按钮,你必须调用
The ViewPager has viewPagerAdapter in this case it's your PojoGroupPagerAdapter class
PojoGroupPagerAdapter has many fragment (it's your PojoFrag class), viewPager display 1 fragment each time.
so when button clicked you must call
int currentItem = viewPageAdapter.getCurrentItem()//current frangment
PojoGroupPagerAdapter adapter = (PojoGroupPagerAdapter ) viewPagerApdater.getAdapter();
PojoFrag pojoFrag = adapter.getItem(currentItem)
您可以使用pojoFrag来获取片段设定值做了你必须打电话的时候,
you can use pojoFrag to get set value for fragment when done you must call
viewPagerAdapter.notifyDataSetChanged()//or adapter of current fragment
这篇关于在ViewPager更新片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!