Search code examples
androidandroid-fragmentsandroid-viewpager

Update Fragment from ViewPager


I have an Activity with a ViewPager. I also have a Dialog that will retrieve a list with some items that the user will choose. Now, how can I update the Fragment where the Items are supposed to display as new views attached to this Fragment?

Here is my ViewPager's adapter:

public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new Fragment1();

        default:
            return null;
        }
    }
}

Now, here is the Fragment:

public class Fragment1 extends Fragment{

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@Override
public ViewGroup onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    ViewGroup view = (ViewGroup)inflater.inflate(R.layout.screen1, container, false);
    }
    return view;
}

}

This is the method that I want to use to add Items to my Fragment

private void addItem() {

    // Instantiate a new "row" view.

    final ViewGroup[] newViews = new ViewGroup[selectedItems.length];


    for(int i = 0; i < selectedItems.length; i++){
        newViews[i] = (ViewGroup) LayoutInflater.from(this).inflate(
                    R.layout.animated_layout_item, mContainerView, false);

        // Set the text in each new row.
        ((TextView) newViews[i].findViewById(android.R.id.text1)).setText(selectedItems[i]);

        // Because mContainerView has android:animateLayoutChanges set to true,
        // adding this view is automatically animated.

            mContainerView.addView(newViews[i], 0);


        // Set a click listener for the "X" button in the row that will remove the row.
        OnClickListener listener = new MyAddItemListener(newViews[i], mContainerView, this);
        newViews[i].findViewById(R.id.delete_button).setOnClickListener(listener);

    }


}

Solution

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