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

孤街浪徒 提交于 2019-12-19 15:24:50

问题


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);
        }
    }
}   

回答1:


PLease use FragmentStatePagerAdapter for that.




回答2:


Your OnPageChangeListener is screwing with the fragments. Every time a page is selected you create a new Fragment and always add it.. your trying to detach a new Fragment that's not attached yet. Its completely messed up. Your SampleFragmentPagerAdapter will handle those fragment transactions for you. So just remove your setOnPageChangeListener and everything should be fine.




回答3:


Here is a simple and working solution for you. Just replace setOnPageChangeListener like this, here we're accessing the instance of current fragment with the help of getSupportFragmentManager and making call to respective fragment method.

    tabsStrip.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            Fragment currentFragment = getSupportFragmentManager()
                    .findFragmentByTag(
                            "android:switcher:" + R.id.viewpager + ":"
                                    + mViewPager.getCurrentItem());
            if (currentFragment instanceof ActiveFragment) {
                ((ActiveFragment) currentFragment).updateFragment();
            } else if (currentFragment instanceof ArchiveFragment) {
                ((ArchiveFragment) currentFragment).updateFragment();
            }
        }
    });

And inside your respective fragment put updateFragment() method.

public void updateFragment() {
    Toast.makeText(getActivity(), "ActiveFragment", Toast.LENGTH_SHORT)
            .show();
    // make updates over here
    // new GetUsersArchivedFromServerTask().execute(); // Network calls
}

Reference link.




回答4:


  1. remove onPageSelected(...) codes from page change listerner, no need attatch/detatch manually, ViewPager take care this automatically. And also your SampleFragmentPagerAdapter should create each Fragment instance in getItem(...).

  2. new GetUsersFromServerTask().execute() should be called in each Fragment's onCreateView and then update the view after Asynctask done.


Besides, you should be aware of OffscreenPageLimit:

public void setOffscreenPageLimit (int limit)

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.




回答5:


Use this inside your fragment.

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser) {
        // Do your code here
    }
}

This method is called every time when the tab is visible to user. Hope this will solve your problem!!!!




回答6:


Try to use notifyDataChange or notifyall




回答7:


Override setPrimaryItem() and call a refresh() method of your fragment at the position. setPrimaryItem() is called every time the selected item changes in viewPager. It may be called more than once so you have to handle it too. see: setPrimaryItem

I hope this helps.



来源:https://stackoverflow.com/questions/30231180/how-to-update-the-list-whenever-the-tabs-are-changing-in-view-pager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!