androidandroid-fragmentsandroid-viewpagerandroid-pageradapter

Refresh current fragment in tabbed layout viewpager


I am trying to refresh the current fragment in the tabbed layout of my viewpager. I have one fragment (UserProgressFragment) that creates the ViewPager and sets the adapter (UserProgressAdapter) along with creating tabbed layout.

In my UserProgressAdapter, in the getItem() method I am returning two other fragments, (UserWeightTrackerFragment and UserCalorieCounterFragment) based on which tab i am on.

My issue is how do i refresh the fragment and update its content/view from the UserCalorieCounterFragment on a button click, because access to the viewpager and adapter are set in the UserProgressFragment? I have considered notifyDataChange for the adapter but i dont know how to call this from this class as it is set up on the UserProgressFragment class. The purpose of wanting to refresh this page is to update a specific view which is a chart, if context is needed.

I have attached the code below:

UserProgressFragment

public class UserProgressFragment extends Fragment{


public static UserProgressFragment newInstance() {
    return new UserProgressFragment();
}

public UserProgressFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_user_progress, container, false);

    ViewPager viewPager = view.findViewById(R.id.progress_viewpager);
    viewPager.setAdapter(new UserProgressTabsAdapter(getChildFragmentManager()));

    TabLayout tabLayout = view.findViewById(R.id.progress_sliding_tabs);
    tabLayout.setupWithViewPager(viewPager, true);

    return view;

    }
}

UserProgressTabsAdapter

public class UserProgressTabsAdapter extends FragmentStatePagerAdapter {

private static final int PAGE_COUNT = 2;
private String tabTitles[] = new String[]{"Weight Tracker", "Calorie Counter"};

public UserProgressTabsAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return UserWeightTrackerFragment.newInstance(position);

        case 1:
            return UserCalorieCounterFragment.newInstance(position + 1);
    }
    return null;
}

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

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    return tabTitles[position];
}

UserCalorieCounterFragment (need to refresh this one)

public class UserCalorieCounterFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {

public static final String ARG_PAGE = "ARG_PAGE";

public static UserCalorieCounterFragment newInstance(int page) {
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, page);
    UserCalorieCounterFragment fragment = new UserCalorieCounterFragment();
    fragment.setArguments(args);
    return fragment;
}

public UserCalorieCounterFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int mPage = getArguments().getInt(ARG_PAGE);
}

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_user_calorie_counter, container, false);

    Button mAddCalories = view.findViewById(R.id.btn_add_calorie);
    mAddCalories.setOnClickListener(this);

    return view;
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btn_add_calorie:
            //REFRESH/UPDATE HERE 
            break;
    }
}

Solution

  • You can create a public method in your fragment which contains the view pager which will contain the notifyDataSetChanged() function. And in the view pager fragment/item, you can call getParentFragment() and type cast it to your parent/first fragment and access that public method to notifyDataSetChanged().