[英]updating Fragments in a ViewPager
我在嘗試更新/替換ViewPager中的片段時遇到問題。 問題在於舊的片段根本不會被新的片段取代。 我已經閱讀了關於stackoverflow的幾個解決方案,並嘗試了它們,但是仍然無法正常工作。 任何人有任何想法為什么? 我將在下面解釋我要做什么。
我有一堂Pojo課
public class Pojo {
public String text;
public Pojo(String text) { this.text = text; }
}
我有一個小組課,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綁定到Fragment。 片段的XML pojogroup_frag.xml如下所示。 如您所見,我將tvTitle設置為PojoGroup.title並將Pojo列表綁定到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適配器,用於將pojos列表綁定到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文件如下所示。
<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有一個Button和一個ViewPager。 我的MainActivity類如下所示。
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如下所示。
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類不是太復雜,此類是關於stackoverflow的一些建議(以前建議進行修改)的地方。 但是,它仍然無法正常工作。 如果需要,我可以提供完整的項目源代碼。
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)。
感謝您提供有關此問題的任何幫助。
這是演示項目的鏈接: http : //www.box.com/s/xbtm3amtkj7f13j2llm4
好的,讓我看看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個片段。
因此,單擊按鈕時,您必須致電
int currentItem = viewPageAdapter.getCurrentItem()//current frangment
PojoGroupPagerAdapter adapter = (PojoGroupPagerAdapter ) viewPagerApdater.getAdapter();
PojoFrag pojoFrag = adapter.getItem(currentItem)
您可以使用pojoFrag完成后獲取片段的設置值,您必須調用
viewPagerAdapter.notifyDataSetChanged()//or adapter of current fragment
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.