Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the List whenever the tabs are changing in view pager

In my app i am using navigation tab using view pager. I have able to successfully drawn the tabs using the code posted on:

https://github.com/codepath/android_guides/wiki/Sliding-Tabs-with-PagerSlidingTabStrip

Here i have used the Sliding tab and view pager both to get the navigation tabs.Everything is working fine but the list are not getting updated when i am moving to the other tab. OnResume() is getting called even the Object list variable is also getting updated while debugging but visually list is not getting updated.

Here i am some snippets of the code:

For Tab1 : which in my case is ACTIVE tab

 @Override
    public void onResume() {
        super.onResume();

        if(!internetUtil.isConnectedToInternet(getActivity())){
            mSwipeRefreshLayout.setEnabled(false);
        }else{
            mSwipeRefreshLayout.setEnabled(true);
            new GetUsersFromServerTask().execute();  // Here i am making the network calls 
        }
}

On Tab2 : Which is Archive tab

 @Override
    public void onResume() {
        super.onResume();
        new GetUsersArchivedFromServerTask().execute(); // Network calls
    }

In MainActivity:

public class MaterialTab extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.material_main_sample);
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager()));
        PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabsStrip.setViewPager(viewPager);
        tabsStrip.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if(position == 0){
                   ActiveFragment activeFragment = new ActiveFragment();
                    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.detach(activeFragment);
                    ft.attach(activeFragment);
                    ft.commit();
                } if(position == 1){
                    ArchiveFragment archiveFragment = new ArchiveFragment();
                    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.detach(archiveFragment);
                    ft.attach(archiveFragment);
                    ft.commit();
                }
            }
        });
    }
}

Also the thing is when i am doing SwipeRefresh then the List is getting updated. I am now totally confused why this behaviour. When i am swiping the tab the same methods are getting called and thelist is not getting updated but when I am doing SwipeRefresh it's getting Updated.

This is the Active Fragment:

public class ActiveFragment extends Fragment {

public void updateFragment(){
        new GetUsersArchivedFromServerTask().execute();
    }

 public class GetUsersArchivedFromServerTask extends AsyncTask<User, Void, String> {
        @Override
        protected String doInBackground(Shipment... parmas) {
            // Log.d(TAG, String.valueOf(shipmentDbHandler.getAllActiveShipments().size()));
            _userList1 = userDbHandler.getAllActiveUserByToday(DateTimeUtil.getCurrentTime());
            return "";
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String str) {
            // Set the refresh Listener to false after the list has been loaded with new set of data
            if (mSwipeRefreshLayout.isRefreshing()) {
                mSwipeRefreshLayout.setRefreshing(false);
            }

            if(_userList1.size() == 0){
                ViewGroup parentOne = (ViewGroup) viewOne.getParent();
                if(parentOne != null){
                    parentOne.removeView(viewOne);
                }
            }

            if(_shipmentList1.size() > 0 ){
                mShipmentAdapter = new ShipmentAdapter(getActivity(),_userList1,1); 
                shipmentListView1.setAdapter(mShipmentAdapter);
                setListViewHeightBasedOnChildren(shipmentListView1);
                ViewGroup parentOne = (ViewGroup) viewOne.getParent();
                if(parentOne == null){
                    mainLayoutOne.addView(viewOne);
                }

                mShipmentAdapter.notifyDataSetChanged();
            }
            mSwipeRefreshLayout.setClickable(true);
        }
    }
}   
like image 712
anand Avatar asked May 14 '15 06:05

anand


People also ask

How do I refresh Fragmentpageradapter?

You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

How do I refresh ViewPager2?

As we already know that ViewPager2 is built on RecyclerView that's why it's easy to update only one item by calling the method notifyItemChanged(index). If we want to update all items then we can also do this by simply calling the notifyDatasetChanged() method. That's it for now.


1 Answers

PLease use FragmentStatePagerAdapter for that.

like image 104
Anand Makwana Avatar answered Nov 15 '22 16:11

Anand Makwana