TabLayout scrolls to unkown position after calling notifyDataSetChanged on PagerAdapter

后端 未结 5 1156
死守一世寂寞
死守一世寂寞 2021-02-14 19:23

I have sample project with TabLayout and PagerAdapter. Strange things happens with TabLayout when I call pagerAdapter.notifyDataSetChanged();

5条回答
  •  有刺的猬
    2021-02-14 20:05

    you can directly set the current postion.

    you can get the selected position before notifying dataset change and save it in static varaible and update it everytime:

    public static tabPostion;
    
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
          tabPostion = tab.getPosition();
        }
    
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
    
        }
    
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
    
        }
    
    //set the postion of the pervious tab
        yourviewpager.setCurrentItem(tabPostion);
    

    below is the code from my project :

       @Override
        public void onTabSelected(TabLayout.Tab tab) {
    
            // imageHashMap = new HashMap<>();
            onClearOrSelectAllImage(FlickQuickEnum.PIC_SELECTION_ACTION.CLEAR);
            selectedImageUrls = new HashMap<>();
            String tag = tab.getText().toString();
            String tagName = tag.substring(1, tag.length());
            currentTab = tagName;
            if (tagName != null) {
                dbAlbumPhotoList = new ArrayList<>();
                if (tagName.equals("All")) {
                    dbAlbumPhotoList = dbAlbumPhotosHashMap.get(album.getAlbumName());
                } else {
                    dbAlbumPhotoList = dbAlbumPhotosHashMap.get(tagName);
                }
            }
            updatePhotoCount();
            setPhotosSelectedActions(false);
        }
    
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
    
        }
    
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
    
        }
    

提交回复
热议问题