简体   繁体   中英

updating Fragments in a 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.

i have a Pojo class

public class Pojo {
 public String text;
 public Pojo(String text) { this.text = text; }
}

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;
 }
}

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>

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);
 }
}

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>

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);
 }
}

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;
 }
}

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();
 }
}

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).

any help with this problem is appreciated.

here is the link to the demo project: http://www.box.com/s/xbtm3amtkj7f13j2llm4

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);
    }
}

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)

you can use pojoFrag to get set value for fragment when done you must call

viewPagerAdapter.notifyDataSetChanged()//or adapter of current fragment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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