繁体   English   中英

在ViewPager中更新片段

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM