Skip to content Skip to sidebar Skip to footer

How To Refresh Recyclerview In One Fragment When Data Changed In Another Fragment

How do I refresh the data from a local database to RecyclerView when data was successfully submitted? I use the tabs on the application. 2nd-Tab functions to submit the data, and i

Solution 1:

Try this,

Create an Interface in EngagedFragment say,

publicinterfaceSubmitListener {

    voidonSubmit();
}

private SubmitListener onSubmitListener;

publicvoidsetSubmitListener(SubmitListener onSubmitListener){
        this.onSubmitListener = onSubmitListener;
}

public SubmitListener getOnSubmitListener(){
        return onSubmitListener;
}

In saveToLocalDB(...) method

call

onSubmitListener.onSubmit();

In MainActivity:

1) Update the below statement:

publicclassMainActivityextendsAppCompatActivityimplementsEngagedFragment.SubmitListener

2) Make your ViewPagerAdapter adapter; variable as Global variable;

while adding fragment:

EngagedFragmentengagedFrag=newEngagedFragment();
adapter.addFrag(engagedFrag, "ENGAGED ID");

...
viewPager.setAdapter(adapter);

3) add this after setting adapter

engagedFrag.setSubmitListener(this);

4) Override onSubmit() method and try the below code in that method

if(viewPager!=null){if(adapter!=null){Fragmentfragment=adapter.getItem(2);if(fragment!=null){RequestFragmentrequestFragment=(RequestFragment)fragment;requestFragment.setRecyclerView();}}}

Solution 2:

try this

mViewPager.setOffscreenPageLimit(0); 

if that doesnt help, please check below SO question:

fragment refresh On Swip

and about : mViewPager.setOffscreenPageLimit(0); this might not work as i found in one of @commonware 's answer's, the following :-

"Does ViewPager require a minimum of 1 offscreen pages Yes. If I am reading the source code correctly, you should be getting a warning about this in LogCat, something like:

Requested offscreen page limit 0 too small; defaulting to 1"

Post a Comment for "How To Refresh Recyclerview In One Fragment When Data Changed In Another Fragment"