Android ViewPager Refresh fragment
Asked Answered
A

2

6

In my app there's 2 tabs (Android Design Support Library) this is the Fragment with ViewPagerAdapter:

public class ListaCanali extends Fragment {

public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2 ;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.lista_canali, container,false);

    //Titolo Fragment
    ((MainActivity) getActivity()).setActionBarTitle("Lista Canali");

    //FindViewById
    tabLayout = (TabLayout) v.findViewById(R.id.tabs);
    viewPager = (ViewPager) v.findViewById(R.id.viewpager);

    //Funzioni Tab
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });

    return v;
}


//Adattatore Tab
class MyAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position)
    {
        switch (position){
            case 0 : return new FragmentCanali();
            case 1 : return new FragmentPreferiti();
        }
        return null;
    }

    public int getCount() {

        return int_items;

    }

    public CharSequence getPageTitle(int position) {

        switch (position){
            case 0 :
                return "Tutti";
            case 1 :
                return "Preferiti";
        }
        return null;
    }

I want my app refresh fragment on Swipe and on click the tabs title because this don't happen, I hope you can help me guys.

Astra answered 7/9, 2015 at 9:25 Comment(2)
you can check this exampleFahlband
Thanks for getChildFragmentManager(). I replaced getFragmentManager() with it in my project.Retraction
S
7

I am sure this is not the only/best way to do it. But this should solve the problem as it did to me.

Please write refresh() methods in both FragmentCanali and FragmentPreferiti Fragments.

    public class ListCanali extends Fragment implements OnPageChangeListener{
    private MyAdapter mAdapter;

    @Override
    public View OnCreateView (...) {
    ...
    if(mAdapter == null) {
    mAdapter = new MyAdapter(getChildFragmentManager());
    }
    viewPager.setAdapter(mAdapter);
    viewPager.setOnPageChangeListener(this);
    ...
    }

    ...
    @Override
    public void OnPageSelected (int position) {
    switch (position){
            case 0 : ((FragmentCanali)adapter.getItem(position)).refresh();
            case 1 : ((FragmentPreferiti)adapter.getItem(position)).refresh();
        }
    }}

It will be cleaner if you extend both the fragments from a base fragment which has abstract refresh() method.

In the onPageSelected() you can just call ((BaseFragment)adapter.getItem(position)).refresh()

Strappado answered 7/9, 2015 at 12:52 Comment(0)
L
6

Try this...

To Refresh a fragment you need it's instance first.

change this

 viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

to

 ArrayList<Fragment> fr_list = new ArrayList<Fragment>();
 fr_list.add(new FragmentCanali());
 fr_list.add(new FragmentPreferiti());
 MyAdapter mMyAdapter = new MyAdapter(getChildFragmentManager(),fr_list);
 viewPager.setAdapter(mMyAdapter);

In adapter change this

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

@Override
public Fragment getItem(int position)
{
    switch (position){
        case 0 : return new FragmentCanali();
        case 1 : return new FragmentPreferiti();
    }
    return null;
}

public int getCount() {

    return int_items;

}

to

 public MyAdapter(FragmentManager fm,ArrayList<Fragment> fr_list) {
    super(fm);
    this.fr_list=fr_list;
}

@Override
public Fragment getItem(int position)
{     
        return fr_list.get(position);
}

public int getCount() {

    return fr_list.size();

}

add refresh() methods inside the fragments and call it whenever you need like below

((FragmentCanali) mMyAdapter.fr_list.get(0)).refresh();
Loos answered 7/9, 2015 at 9:40 Comment(5)
hello, thank you but show me error... screen: Click HEREAstra
Ok i solved but i have new error: CLICK HEREAstra
add this line in your adapter ArrayList<Fragment> fr_list;Loos
i have already add the line look at the screen. What's fr_list? Can you edit and put my fragment in the example?Astra
Change ((FragmentCanali)fr_list.get(0)).refresh(); to ((FragmentCanali)mMyAdapter.fr_list.get(0)).refresh();, where mMyAdapter is your member variable for the adapter.Devereux

© 2022 - 2024 — McMap. All rights reserved.