Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Handle Sliding Tab layout properly

    • 0
    • 3
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.11k
    Comment on it

    Hello guys, as we all have worked on Tabs widget and Tabs host but now Tab widget and host have been deprecated. Android lollipop provides a new interface to implement Tabs that are SlidingTabLayout.java & SlidingTabStrip.java classes. These features basically discovered for Material design but we can also use for another versions of Android.

    Problems with Sliding tabs :-
    1. Sliding tabs are easy to implement but difficult to customize and difficult to maintain like sometime we want to show some images with some text in tabs.

    2.Sometimes when you are using Fragment pager adapter then you ll face a problem like you are swiping from Fragment A to Fragment B and then Fragment B to Fragment C that ll work fine but when you ll go back from Fragment C to Fragment B then life cycle of Fragment A ll also be called.So you have to managed that.

    Solutions : Here i am sharing my solution of both problems here :-

    1. In your sliding tab layout file you have to change the functionality of populateTabStrip() like this
    private void populateTabStrip() {
        final PagerAdapter adapter = mViewPager.getAdapter();
        final OnClickListener tabClickListener = new TabClickListener();
    
        for (int i = 0; i < adapter.getCount(); i++) {
            View tabView = null;
    
            if (i == 0) {
                tabView = LayoutInflater.from(getContext()).inflate(R.layout.custom_tab1, mTabStrip,
                        false);
                Drawable draw = getResources().getDrawable(getIconResourceArray()[i]);
    
                ImageView iconImageView = (ImageView) tabView.findViewById(R.id.imgTab1);
                iconImageView.setImageDrawable(draw);
    
            } else if (i == 1) {
                tabView = LayoutInflater.from(getContext()).inflate(R.layout.custom_tab2 mTabStrip,
                        false);
                Drawable draw = getResources().getDrawable(getIconResourceArray()[i]);
    
                ImageView iconImageView = (ImageView) tabView.findViewById(R.id.imgTab1);
                TextView name = (TextView) tbView.findViewById(R.id.text);
                name.setText("Tab2");
                iconImageView.setImageDrawable(draw);
    
            } else {
                tabView = LayoutInflater.from(getContext()).inflate(R.layout.custom_tab3, mTabStrip,
                        false);
                Drawable draw = getResources().getDrawable(getIconResourceArray()[i]);
    
                ImageView iconImageView = (ImageView) tabView.findViewById(R.id.imgTab1);
                iconImageView.setImageDrawable(draw);
            }
            tabView.setOnClickListener(tabClickListener);
    
            mTabStrip.addView(tabView);
        }
    }
    **2.Handle Tabs on Swiping :**
    setUserVisibleHint(boolean status) :- Set a hint to the system about whether this fragment's UI is currently visible to the user.
    An app may set this to false to indicate that the fragment's UI is scrolled out of visibility or is otherwise not directly visible to the user. This may be used by the system to prioritize operations such as fragment lifecycle updates or loader ordering behavior.

    so override this method in your Fragment A,B,C like this

        private boolean attachOrNot = false;
     @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            attachOrNot = isVisibleToUser;
    
            if(attachOrNot) {
    
                if (getView() != null){
                     fetchSomeRecordHere();
                }
    
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            inflater = LayoutInflater.from(getActivity());
    
            view = inflater.inflate(R.layout.fragment_save, container, false);
    
            if(attachOrNot) 
            fetchSomeRecordHere();
    
            return view;
        }
    

    So this will be called only once when current tab fragment will be visible.so always use this in your tab fragment class.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: