Update Data in Listfragment as Part of Viewpager

Update data in ListFragment as part of ViewPager

Try to record the tag each time a Fragement is instantiated.

public class MPagerAdapter extends FragmentPagerAdapter {
private Map<Integer, String> mFragmentTags;
private FragmentManager mFragmentManager;

public MPagerAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
mFragmentTags = new HashMap<Integer, String>();
}

@Override
public int getCount() {
return 10;
}

@Override
public Fragment getItem(int position) {
return Fragment.instantiate(mContext, AFragment.class.getName(), null);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Object obj = super.instantiateItem(container, position);
if (obj instanceof Fragment) {
// record the fragment tag here.
Fragment f = (Fragment) obj;
String tag = f.getTag();
mFragmentTags.put(position, tag);
}
return obj;
}

public Fragment getFragment(int position) {
String tag = mFragmentTags.get(position);
if (tag == null)
return null;
return mFragmentManager.findFragmentByTag(tag);
}
}

update a listview in a fragment in a viewpager from activity

What you should do is create a public method for your Fragment1 and Fragment2 like so:

define in your Activity:

Fragment1 frag1;
Fragment2 frag2;

then your ViewPager:

public class SubAdapter extends FragmentPagerAdapter
{
public SubGroupAdapter(FragmentManager fm, data data)
{
super(fm);
}

@Override
public Fragment getItem(int position)
{
Bundle bundle = new Bundle();
bundle.putString("data", data);

switch (position)
{
case 0:
frag1 = new Fragment1();
frag1.setArguments(bundle);
return frag1;

case 1:
frag2 = new Fragment2();
frag2.setArguments(bundle);
return frag2;
}

return null;
}
}

Put this method in Fragment1.java :

public void updateFragment1ListView(){
if(adapter != null){
adapter.notifyDataSetChanged();
}
}

and from your activity call:

  if(frag1 != null){
frag1.updateFragment1ListView();
}

obviously change the names for your adapter if its not called adapter...

Just do the same for Fragment2.java as well

Update ListView in Viewpager

ContentFragment frag = (ContentFragment) adapter.getItem(pos);

Never use getItem() function of the adapter to get the current fragment from ViewPager. It is called internally to instantiate the fragment and we are not supposed to call it explicitly. The Fragment returned by calling getItem is not the actual fragment present in ViewPager at that position. So update call doesn't update the data in intended fragment.

Try using one of the methods discussed in this link.

Update listFragment when camera changes in MapFragment

You could try making an Interface with an update method like so

public interface UpdatingFragment {
public void update();}

Then have your ListFragment implement this interface. And in the update method recreate the list in your ListFragment.

public class MyListFragment extends ListFragment implements UpdatingFragment{

@override
public void update(){
//whatever code you use to update your fragment
}

Then call this method from your PagerAdapter

@Override
public int getItemPosition(Object item) {
if (item instanceof UpdatingFragment) {
((UpdatingFragment) item).update();
}
//don't recreate fragment
return super.getItemPosition(item);
}

Now if you call notifyDataSetChanged() on your viewpager adapter, it should hopefully update your ListFragment without affecting the map. Call this every time the user moves the camera in the map fragment.

Update ListFragment from ViewPager with new content

Using this answer - get a reference on your fragment and call proper method (depends on your realization of getting data in your ListView in ListFragment) in your onPageSelected method.

Update Fragment from ViewPager

Update Fragment from ViewPager

You need to implement getItemPosition(Object obj) method.

This method is called when you call

notifyDataSetChanged()

on your ViewPagerAdaper. Implicitly this method returns POSITION_UNCHANGED value that means something like this:
"Fragment is where it should be so don't change anything."

So if you need to update Fragment you can do it with:

  • Always return POSITION_NONE from getItemPosition() method. It which
    means: "Fragment must be always recreated"
  • You can create some update() method that will update your
    Fragment(fragment will handle updates itself)

Example of second approach:

public interface Updateable {
public void update();
}


public class MyFragment extends Fragment implements Updateable {

...

public void update() {
// do your stuff
}
}

And in FragmentPagerAdapter you'll do something like this:

@Override
public int getItemPosition(Object object) {
MyFragment f = (MyFragment ) object;
if (f != null) {
f.update();
}
return super.getItemPosition(object);
}

And if you'll choose first approach it can looks like:

@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}

Note: It's worth to think a about which approach you'll pick up.



Related Topics



Leave a reply



Submit