Search code examples
androidandroid-viewpagerandroid-tabsviewpagerindicatorandroid-actionbar-compat

PagerSlidingTabStrip and onTabUnselect


i use PagerSlidingTabStrip from

https://github.com/astuetz/PagerSlidingTabStrip/blob/master/library/src/com/astuetz/PagerSlidingTabStrip.java

i am implementing an app that when user select a tab i send a request that corresponds to that tab to the server. i want to cancel those requests when the user select other tabs. i mean for example user select tab1 and i request to server called req1 now when user select tab 2 i want to cancel req1 and send req2 to server. in tab implementation of action bar there is a call back method call onTabUnselected and i want to implement that and integrate it to that library but i can not find where i must call that method. can anyone help me?


Solution

  • PagerSlidingTabStrip has nothing to do with the Tabs related to an ActionBar afaik, so onTabUnselected won't be called when de-selecting tabs in the PagerSlidingTabStrip. Also Tabs related to the ActionBar is deprecated in Android L.

    I'd recommend you create a new variable that keeps track of the last selected tab (when using the OnClickListener on a tab (see line 251 in the PagerSlidingTabStrip class).

    An example of some (not tested) code:

    int lastClickedTabPosition;
    
    // This method should be called from the OnClickListener of the tabs.
    private void saveClickedPositionAndCancelLastRequest(int clickedPosition) {
        // Cancel your last request here by using the lastClickedTabPosition to get the last clicked position if you have more requests sent to the server.
        // Afterwards set lastClickedTabPosition with the new clicked position:
        this.lastClickedTabPosition = clickedPosition;
    }
    

    This variable could hold the last known clicked tab position. Whenever you click a new tab, you simply get the last clicked tab's position and then cancel the last request send.

    On a side-note: Google has created their own implementation of the PagerSlidingTabStrip called SlidingTabLayout and has opensourced it here: https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html, which might be easier to use and especially get help with in the future :-)

    Hope this helps.